Data Binding and Commands
WPF controls such as toggle buttons can follow the formatting at the input position of the TextControl through data binding, without code-behind. When a binding doesn't update, or a button doesn't show what you expect, the cause is usually which side of the binding a property can be on, or a value that is set twice. Data Binding explains how binding with the TextControl works.
A Button Doesn't Follow the Formatting
A toggle button bound to the formatting of the text doesn't change when the input position moves into bold text, or clicking it doesn't make the text bold.
The source of the binding must be the formatting at the input position: TextControl.InputFormat returns an InputFormat object, whose properties are updated automatically when the input position changes. For the button to change the text as well, the binding must work in both directions.
- Bind the target property, such as
IsChecked, to a property ofInputFormat, such as InputFormat.Bold, through the name of theTextControl. - Set
ModetoTwoWay, so that the button changes the formatting. - Set
Focusableof the button toFalse, so that clicking it doesn't take the focus from the text.
Bind to the Formatting at the Input Position shows the complete XAML.
The Text of a Toggle Button Doesn't Change
A toggle button should show another text when it is checked, for example "Bold" and "UnBold", but it always shows the same text.
A value set directly on the element takes precedence over a style. When the button sets Content as an attribute, a trigger of its style can't change it.
Remove the
Contentattribute from theToggleButtonelement.Set the text in a
Setterof the style, and change it in a trigger:<ToggleButton Name="tbtnBold" Focusable="False" IsChecked="{Binding ElementName=textControl1, Path=InputFormat.Bold, Mode=TwoWay}"> <ToggleButton.Style> <Style TargetType="{x:Type ToggleButton}"> <Setter Property="Content" Value="Bold"/> <Style.Triggers> <Trigger Property="IsChecked" Value="True"> <Setter Property="Content" Value="UnBold"/> </Trigger> </Style.Triggers> </Style> </ToggleButton.Style> </ToggleButton>
A Property of the TextControl Can't Be a Binding Target
A binding whose target is a property of the TextControl doesn't work, while a binding with the same property as its source does.
The target of a WPF binding must be a dependency property; the source can be any property. The reference marks the dependency properties of the TextControl, such as ZoomFactor and IsUndoEnabled, with "This is a dependency property." Properties such as Text aren't described as dependency properties, and CanUndo and CanRedo are read-only.
- Check the reference page of the property: bind to it as a target only if it is a dependency property.
- Use other properties as the source of a binding, as the TX Words samples do: they enable their undo and redo buttons with bindings to
CanUndoandCanRedo. - Values that a WPF control can't show directly, such as colors of
System.Drawingor distances in twips, need one of the value converters of TX Text Control. See Value Converters.
Commands Don't Reach the TextControl
You want to connect WPF commands to the TextControl, but no source documents command support of TX Text Control: the reference, the guides and the samples use event handlers and bindings instead. Commands and Input Bindings describes how to connect the user interface of your application, and MVVM Integration Examples lists the building blocks for an MVVM application.