# Data Binding and Commands

> Fix data binding problems with TX Text Control .NET for WPF: bindings to InputFormat, Content set twice, properties that can't be targets, and commands.

- **Product:** TX Text Control .NET for WPF
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for WPF > Troubleshooting > Data Binding and Commands
- **1340 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/troubleshooting/data-binding/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/troubleshooting/data-binding/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/troubleshooting/data-binding/llms-full.txt

---

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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/integration/data-binding/llms-full.txt) 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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/inputformat-property/llms-full.txt) returns an [InputFormat](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/inputformat/llms-full.txt) 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.

1. Bind the target property, such as `IsChecked`, to a property of `InputFormat`, such as [InputFormat.Bold](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/inputformat/bold-property/llms-full.txt), through the name of the `TextControl`.
2. Set `Mode` to `TwoWay`, so that the button changes the formatting.
3. Set `Focusable` of the button to `False`, so that clicking it doesn't take the focus from the text.

[Bind to the Formatting at the Input Position](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/integration/data-binding/document-content/#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.

1. Remove the `Content` attribute from the `ToggleButton` element.
2. Set the text in a `Setter` of the style, and change it in a trigger:

    ```xml
    <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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/zoomfactor-property/llms-full.txt) and [IsUndoEnabled](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/isundoenabled-property/llms-full.txt), with "This is a dependency property." Properties such as [Text](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/text-property/llms-full.txt) aren't described as dependency properties, and [CanUndo](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/canundo-property/llms-full.txt) and [CanRedo](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/canredo-property/llms-full.txt) are read-only.

1. Check the reference page of the property: bind to it as a target only if it is a dependency property.
2. 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 `CanUndo` and `CanRedo`.
3. Values that a WPF control can't show directly, such as colors of `System.Drawing` or distances in twips, need one of the value converters of TX Text Control. See [Value Converters](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/integration/data-binding/value-converters/llms-full.txt).

### 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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/integration/commands/llms-full.txt) describes how to connect the user interface of your application, and [MVVM Integration Examples](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/examples/mvvm/llms-full.txt) lists the building blocks for an MVVM application.

### Related Pages

- [Data Binding](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/integration/data-binding/llms-full.txt): bindings to the `TextControl`.
- [XAML and Data Binding Examples](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/examples/xaml-data-binding/llms-full.txt): the XAML of the bindings and converters.
- [Control Initialization](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/troubleshooting/initialization/llms-full.txt): code that runs before the control is ready.

---

## Nearby

- **Previous:** [Control Initialization](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/troubleshooting/initialization/llms-full.txt)
- **Next:** [Dispatcher and Thread Access](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/troubleshooting/dispatcher/llms-full.txt)
