XAML and Data Binding Examples
WPF controls such as toggle buttons, sliders and text boxes can follow the formatting at the input position of the TextControl through data binding, without code-behind. Text Control shows this in a technical article and in the XAML examples of the reference, and the TX Words samples bind their undo buttons this way. No sample project is dedicated to data binding; the examples below are the places where the XAML is shown.
TextControl in XAML
Every sample of TX Text Control .NET for WPF declares the TextControl in XAML, with a prefix for the namespace TXTextControl.WPF such as WPF or my. The smallest sample that does this is Simple in the folder Tutorial of TXSamples_WPF_CSharp, written for version 32.0. Add TextControl in XAML explains the namespace declaration.
Bind a Button to the Formatting
The technical article on data binding with TX Text Control binds a ToggleButton to InputFormat.Bold, the formatting at the input position: the button shows whether the text at the input position is bold, and clicking it switches bold formatting on or off. Bind to the Formatting at the Input Position shows this XAML in a complete window and explains each part of the binding. The blog article Get a Foretaste of What's to Come: WPF Data Binding with TX Text Control shows the same example, together with a style trigger that changes the text of the button.
Value Converter Examples
Some properties of the TextControl have a type or a unit that a WPF control can't show directly. Four value converters of the namespace TXTextControl.WPF translate them, and the reference page of each one contains a XAML example:
| Example | What it shows | Where |
|---|---|---|
| Radio buttons for list formats | BooleanConverter checks a radio button when InputFormat.NumberedListFormat has the value of its converter parameter | BooleanConverter |
| A color swatch | ColorConverter converts InputFormat.TextColor, a System.Drawing.Color, to the color of a WPF brush | ColorConverter |
| A zoom slider | LogarithmicConverter maps a ZoomFactor from 10 to 400 percent to a slider, with 100 percent in the middle | LogarithmicConverter |
| A distance in centimeters | MeasureConverter shows InputFormat.TopDistance, which is in twips, in a text box in the unit that Unit sets | MeasureConverter |
Value Converters describes the converters and shows their XAML.
Bindings in the Samples
| Example | What it shows | Where |
|---|---|---|
| Undo and redo buttons | The buttons of the quick access toolbar are enabled through bindings to CanUndo and CanRedo of the TextControl. Version 32.0, the .NET Framework 4.8 and .NET 6 | TXSamples_WPF_CSharp, the ribbon samples in the folder TX Words |
The ribbon tabs of TX Text Control are connected to the TextControl in XAML as well; Ribbon and Sidebar Examples shows how.
What No Sample Shows
No sample binds the content of a document, such as its text, to a property of your own. MVVM Integration Examples says why, and Binding Document Content covers the content of a document and your data.
Next Steps
- Data Binding: how data binding works with the
TextControl. - MVVM Integration Examples: the building blocks for an MVVM application.
- XAML Integration: declare and configure the controls in XAML.