TX Text Control .NET for WPF

XAML Integration

Topic

A WPF window with TX Text Control is mostly XAML: the editor, its ribbon tabs, bars and sidebars are elements, their settings are attributes, and they find each other by name. Knowing the few kinds of XAML entries the product uses lets you read what the toolbox writes, write a window by hand and spot a connection that doesn't work. Each kind is shown below, then all of them together in one window.

If you haven't placed a TextControl in XAML yet, Add TextControl in XAML starts with the minimal window.

The Kinds of XAML Entries

EntryExampleUsed for
Namespace declarationxmlns:WPF="clr-namespace:TXTextControl.WPF;assembly=TXTextControl.WPF"Mapping the prefix WPF to the controls of TX Text Control
Element<WPF:TextControl x:Name="textControl1" />The editor, the bars, the ribbon tabs and the sidebars
AttributeViewMode="PageView"Properties with simple values, such as enumeration values, numbers and strings
Connection by nameStatusBar="statusBar1"Connecting the editor to a bar, a ribbon tab or a sidebar
Property element<WPF:TextControl.DisplayColors>Properties whose value is an object, such as TextControlColors
Event attributeLoaded="textControl1_Loaded"Handlers in the code-behind, for WPF events and events of TX Text Control
Resource<spell:TXSpellChecker x:Key="txSpellChecker1" />Objects that aren't shown, such as the spell checker of TX Spell .NET

All controls of TX Text Control are in the namespace TXTextControl.WPF of the assembly TXTextControl.WPF, so one declaration covers the whole window. The Visual Studio toolbox writes it with the prefix WPF, and the samples here use the same prefix. Assemblies and XAML Namespaces lists the other namespaces, such as those for drawings and barcodes.

Connections Are Names

The WPF controls of TX Text Control don't hold references to each other. Each connection property is a string with the x:Name of the element it connects to, and an empty string, the default, means that nothing is connected:

Because they are plain strings, write the names without {Binding} or {StaticResource}. The compiler doesn't check them: a misspelled name leaves the bar unconnected without an error, and renaming an element in the XAML means updating every property that names it. Keep the connected elements in the same window or user control as the TextControl, where their names are defined.

A Complete Window

The following MainWindow.xaml uses every kind of entry: a ribbon with two tabs, a ruler, a status bar, a styles sidebar connected to the Home tab, and the editor in page view on a light gray background:

<Window x:Class="MyWPFApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:WPF="clr-namespace:TXTextControl.WPF;assembly=TXTextControl.WPF"
        Title="MainWindow" Height="600" Width="1000">
    <DockPanel>
        <!-- The ribbon is a WPF control; the tabs come from TX Text Control -->
        <Ribbon x:Name="ribbon1" DockPanel.Dock="Top">
            <WPF:RibbonFormattingTab x:Name="ribbonFormattingTab1"
                                     StylesSidebar="stylesSidebar1" />
            <WPF:RibbonInsertTab x:Name="ribbonInsertTab1" />
        </Ribbon>

        <WPF:StatusBar x:Name="statusBar1" DockPanel.Dock="Bottom" />
        <WPF:RulerBar x:Name="rulerBar1" DockPanel.Dock="Top" />

        <!-- A sidebar names its editor, too -->
        <WPF:Sidebar x:Name="stylesSidebar1" DockPanel.Dock="Right" Width="250"
                     ContentLayout="Styles" TextControl="textControl1" />

        <WPF:TextControl x:Name="textControl1"
                         RibbonFormattingTab="ribbonFormattingTab1"
                         RibbonInsertTab="ribbonInsertTab1"
                         RulerBar="rulerBar1"
                         StatusBar="statusBar1"
                         ViewMode="PageView"
                         Loaded="textControl1_Loaded"
                         Changed="textControl1_Changed">
            <WPF:TextControl.DisplayColors>
                <WPF:TextControlColors DesktopColor="#F5F6F7" />
            </WPF:TextControl.DisplayColors>
        </WPF:TextControl>
    </DockPanel>
</Window>

The XAML works as follows:

  • Ribbon has no prefix: it is the ribbon of WPF, in System.Windows.Controls.Ribbon. The tabs inside it, RibbonFormattingTab and RibbonInsertTab, are predefined tabs of TX Text Control, which the TextControl finds by their names.
  • DockPanel.Dock is an attached property of the WPF DockPanel: the ribbon and the ruler go to the top, the status bar to the bottom, the sidebar to the right, and the TextControl, the last child, fills the rest. WPF Layout and Resizing covers other panels.
  • ContentLayout="Styles" chooses the panel of the sidebar; XAML converts the text into the value of the enumeration, as for ViewMode="PageView". StylesSidebar="stylesSidebar1" connects the sidebar to the Change Styles item of the style list on the Home tab.
  • WPF:TextControl.DisplayColors, the property element, and the event attributes Loaded and Changed work as in Set Properties and Events in XAML, and the two handlers are the methods of Handle the Events in the Code-Behind, in MainWindow.xaml.cs.

The ribbon tabs are part of the Enterprise and Professional editions; see Editions and Feature Availability.

What XAML Can't Set

Some members of the TextControl exist only at run time, because they describe the document rather than the control: Selection, the collections such as Tables, Images and Barcodes, and state such as CanUndo. The reference marks them "Runtime only". Use them in code, once the control is loaded; Control Initialization and Document Access explains when that is. Document content, such as a template the editor starts with, is loaded in code as well, or bound through the data binding support of TX Text Control; see Data Binding.

The XAML Designer

The XAML Designer of Visual Studio shows the window as you edit the XAML, once the project has been built. In its Properties window, the connection properties of a selected TextControl are text boxes into which you type the name of the bar or tab. Visual Studio Toolbox and XAML Designer describes the toolbox, the designer and the restart it asks for when the first control is added, and Toolbox and XAML Designer in Troubleshooting what to do when a bar isn't connected.

Next Steps