Data Binding
Data binding is how WPF connects controls to data without event handlers, and the TextControl takes part in it on both sides: some of its properties can be set by a binding, and many of its values can feed one. Knowing which is which tells you how toolbars and view models bind to the editor, and what to do where no binding exists, such as for the document content.
How Binding Works With the TextControl
A WPF binding has a target, which must be a dependency property, and a source, which can be any property. For the TextControl, this means:
- The editor as a target. A view model or another control can set the dependency properties of the TextControl through a binding, such as ZoomFactor, ViewMode and IsUndoEnabled. What a Style Can Set lists all of them.
- The editor as a source. Every property can feed a binding. The most useful source is InputFormat, the formatting at the input position, which updates itself when the caret moves. CanUndo and CanRedo enable buttons.
- Properties that are not dependency properties, such as EditMode, DisplayColors and Text, are set in XAML or in code, not by a binding.
- Connections to the bars are names, not bindings.
ButtonBar="buttonBar1",RulerBar,StatusBarand the ribbon tab properties take the name of the other control as a string. - The document has no bindable property. Its content moves in and out with
LoadandSave; an attached property of your own can wrap them for a binding.
Values in twips, System.Drawing.Color values and enumeration values reach WPF controls through the value converters of TX Text Control.
The Mode of a binding decides in which direction the value flows. A toggle button bound to InputFormat.Bold needs TwoWay, because its click has to change the text:
| Mode | The value flows |
|---|---|
OneWay | From the source to the target, whenever the source changes |
TwoWay | In both directions, whenever the source or the target changes |
OneTime | From the source to the target once, when the binding starts |
OneWayToSource | From the target to the source, whenever the target changes |
Find the Right Page
| If you want to | Read |
|---|---|
| Bind toolbar controls to the formatting at the input position | Binding Document Content |
| Bind the whole document to a property of a view model | Binding Document Content |
| Show font sizes in points, colors in a swatch or a zoom slider | Value Converters |
| Reach the element of the template that shows the document | TextControlDocument |
| Structure the application with a view model and commands | MVVM Integration Patterns |
| Find out why a binding doesn't work | Data Binding and Commands in Troubleshooting |
Pages in This Section
- TextControlDocument: the
Canvasin the template of theTextControlthat shows the document, and how to get it. - Binding Document Content: bindings to
InputFormatandCanUndo, and an attached property that binds the whole document. - Value Converters:
MeasureConverter,ColorConverter,BooleanConverterandLogarithmicConverterin bindings.