TX Text Control .NET for WPF

Data Binding

Topic

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, StatusBar and 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 Load and Save; 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:

ModeThe value flows
OneWayFrom the source to the target, whenever the source changes
TwoWayIn both directions, whenever the source or the target changes
OneTimeFrom the source to the target once, when the binding starts
OneWayToSourceFrom the target to the source, whenever the target changes

Find the Right Page

If you want toRead
Bind toolbar controls to the formatting at the input positionBinding Document Content
Bind the whole document to a property of a view modelBinding Document Content
Show font sizes in points, colors in a swatch or a zoom sliderValue Converters
Reach the element of the template that shows the documentTextControlDocument
Structure the application with a view model and commandsMVVM Integration Patterns
Find out why a binding doesn't workData Binding and Commands in Troubleshooting

Pages in This Section

  • TextControlDocument: the Canvas in the template of the TextControl that shows the document, and how to get it.
  • Binding Document Content: bindings to InputFormat and CanUndo, and an attached property that binds the whole document.
  • Value Converters: MeasureConverter, ColorConverter, BooleanConverter and LogarithmicConverter in bindings.