BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Data Binding - Common Binding Options

The ninety-fourth part of the Windows Presentation Foundation Fundamentals tutorial adds to the description of data binding. This article describes two common options for data binding that allow control of the direction of data flow and the timing of updates.

Setting the Binding Mode

You can use the binding mode option to determine the directions in which data passes between the binding source and the target control. Five options are available, each defined in the BindingMode enumeration:

  • TwoWay. Configures the binding to be bi-directional. Changes made by the user are passed back to the data source and changes in the source update the control. This option is generally used for user input controls.
  • OneWay. Sets the binding so that changes made in the data source are copied into the bound property of the target control. Updates made by the user are not passed to the data source. This binding mode is generally used for read-only controls, such as TextBlocks.
  • OneWayToSource. Configures the binding so that changes made by manipulating the control are passed back to the data source. Changes in the data source are not copied into the control.
  • OneTime. A one-time data binding means that the control's property is set when control is created or when the data context is changed. Further changes to either the property or the data source are not transmitted. This type of binding is generally used for static data or when you wish to display a snapshot of the data at a point in time.
  • Default. Uses the default binding mode for the property. This value varies according to the control. For example, a TextBlock's Text property defaults to being one-way but a TextBox's Text property uses a two-way binding as standard.

TwoWay Binding

Let's try each of the four key binding modes, starting with TwoWay. Modify the XAML for the first TextBox to add the Mode option. Note how it follows the path and is separated from it using a comma:

<TextBox Grid.Column="1" Height="22" Text="{Binding Text,Mode=TwoWay}"/>

If you run the program you will see that the first text box has already read the value from the source. Change the text in the text box. When the control loses focus, the TextBlock at the bottom of the window updates, showing that the information entered is copied back to the source.

OneWay Binding

Next, let's change the second text box so that it uses one way binding. Update the control's XAML:

<TextBox Grid.Column="1" Grid.Row="1" Height="22" Text="{Binding Text,Mode=OneWay}"/>

As the one way binding only reads from the source, you'll see that the text box is initialised on loading. Its contents also change if you edit the text in the first text box. However, changing the text in the one-way bound text box does not update the source, so the other controls remain unchanged.

OneWayToSource Binding

For the third box we'll use one way to source binding mode:

<TextBox Grid.Column="1" Grid.Row="2" Height="22"
         Text="{Binding Text,Mode=OneWayToSource}"/>

The new binding has several effects. Firstly when you run the program, all of the text boxes are now empty. This is because the text box has no text and passes this information back to the data source when the binding is established. If you add text to the control, it updates in the data object. However, changes made in other text boxes that update the source are not shown.

OneTime Binding

We'll use the OneTime binding for the fourth TextBox.

<TextBox Grid.Column="1" Grid.Row="3" Height="22" Text="{Binding Text,Mode=OneTime}"/>

This binding causes the text box to be initialised on loading. No changes in the data source or the Text property are transmitted.

Update Triggers

The update trigger for a data binding determines when changes made in the control's property are passed back to the data source. They are valid only for one-way and two-way bindings. Four options are available, each defined in the UpdateSourceTrigger enumeration.

LostFocus. This mode causes changes in the property of a control to be copied to the data source when the control loses focus. It is useful for controls that receive many updates and the data source only needs to know the end result. For example, it is often unnecessary for a TextBox's Text property to be copied to the source after every key press.

PropertyChanged. If you use this mode, changes to the information in the control are copied to the source immediately. This is useful for controls such as CheckBoxes, where the update should happen when the control is clicked, not when it loses focus.

Explicit. When a data binding's update trigger is set to Explicit, changes to the property are not copied automatically. You must call the data binding's UpdateSource method.

Default. As with the binding mode, controls use different options for the update trigger. Setting the update trigger to Default uses the standard option for the property.

8 September 2014