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.

LostFocus Update Trigger

The default update trigger mode for a TextBox is to change the source when the control loses focus. However, to demonstrate the syntax, let's explicitly set the trigger and the mode for the fifth text box:

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

Run the program to see the results. The fifth text box will behave in exactly the same manner as the first.

PropertyChanged Update Trigger

Let's update the sixth TextBox control so that it uses the PropertyChanged option:

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

Run the program and enter text into the newly bound text box. You'll find that the source data is updated with each change to the text. It's no longer necessary to change the focus to perform the update.

Explicit Update Trigger

If the LostFocus and PropertyChanged options do not meet your requirements, you can use explicit updates. Automatic changes to the source data are disabled with this option. You must force updates using the binding's UpdateSource method.

To demonstrate, set the final TextBox's XAML to use the explicit update trigger option:

<TextBox Name="Explicit" Grid.Column="1" Grid.Row="6" Height="22" Margin="0 0 49 0"
         Text="{Binding Text, Mode=TwoWay,UpdateSourceTrigger=Explicit}"/>

We'll force updates to the binding of the last text box when the button is clicked. Register the Click event for the button, as follows:

<Button Grid.Column="1" Grid.Row="6"
        Height="22" Width="50" HorizontalAlignment="Right"
        Content="Update" Click="Button_Click"/>

We can now add code behind the window to call the UpdateSource method. Firstly we need to obtain the binding details in a BindingExpression object. This is achieved by calling the control's GetBindingExpression method, passing the dependency property involved in the binding. Once you have the binding expression, you can call the method.

Add the following code for the button's Click event. Note the parameter used when obtaining the binding expression. Each dependency property for a control type has a corresponding static property in the control's class. In this case, TextBox.TextProperty indicates that we wish to obtain the data binding related to the Text property of a TextBox.

private void Button_Click(object sender, RoutedEventArgs e)
    BindingExpression expr = Explicit.GetBindingExpression(TextBox.TextProperty);

Run the program for a final time to test the new binding. You will find that changing the text in the final text box does not affect the data source and the other controls until you click the button. Updates in other text boxes are still displayed in the control automatically.

8 September 2014