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 - Fallback Values

The ninety-fifth part of the Windows Presentation Foundation Fundamentals tutorial continues the examination of data binding. This article considers the values assigned to properties when a binding operation fails.

Data Binding

In previous articles we have looked at several aspects of data binding in WPF. We've looked at binding dependency properties of controls to properties of other controls or to members of an object that is set as a control's data context. In all examples we've looked at bindings that are working correctly. We haven't considered what happens when a binding fails.

For a data binding to work correctly and provide a control with a value, three things must be true.

  1. The source and path for the binding must resolve correctly.
  2. The value provided by the source must be of a compatible type with the target property, or a converter must be specified that converts the source to a compatible type. We will look at converters in a future article in this tutorial.
  3. If a converter is used, the value from the source of the data binding must be compatible with that converter.

If any of the above prerequisites are not met, the data binding will fail. Usually this leaves the target property with its default value. However, you can specify a fallback value. This is a value that is compatible with the target property and that will be used when the data binding is invalid.

To demonstrate the use of fallback values we need a sample project. Create a new WPF application project in Visual Studio named, "FallbackValueDemo". Once prepared, replace the XAML of the main window with the following:

<Window x:Class="FallbackValueDemo.MainWindow"
        Title="Fallback Value Demo"
        <TextBox Name="BindingSource" Text="5" Margin="10" />
        <Slider Minimum="0" Maximum="10" Margin="10"
                Value="{Binding ElementName=BindingSource,Path=Text}" />

This very simple example window includes a Slider and a TextBox. The Slider's Value property is bound to the TextBox's Text. When you run the program and move the slider, the text in the text box is updated to show the value. As this is a two-way binding, entering a number between zero and ten into the text box updates the slider to match.

Data binding demo with valid binding

The Text property has a much wider range of possible values than the slider's Value, allowing letters and symbols. If you enter a non-numeric value, this cannot be transferred to the slider. The data binding then fails and the slider value defaults to zero, moving the thumb part of the control to the far left, as shown below.

Data binding demo with invalid binding

Adding a Fallback Value

If you wish to add a fallback value, you include the FallbackValue setting in the details of the binding. For basic binding expressions, this is separated from other parts of the binding using a comma.

To demonstrate, modify the XAML for the Slider as shown below. This sets the fallback value to five, which places the slider thumb into the middle of the control when the binding fails.

<Slider Minimum="0" Maximum="10" Margin="10"
        Value="{Binding ElementName=BindingSource,Path=Text,FallbackValue=5}" />

Try running the program again. Moving the slider continues to update the text box as before. If you enter a non-numeric value into the text box, the slider uses the fallback value.

Property Element Syntax

As with other bindings mentioned earlier in the tutorial, you can declare a data binding that contains a fallback value using property element syntax. This allows you to provide more complex values and objects.

To demonstrate, modify the code for the slider as shown below. The binding is functionally identical but uses the more verbose syntax.

<Slider Minimum="0" Maximum="10" Margin="10">
11 September 2014