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 - Priority Bindings

The one hundredth part of the Windows Presentation Foundation Fundamentals tutorial expands upon data binding with priority bindings. These allow a single binding source to be selected from a number of possible options.

Data Binding

In recent articles in the WPF tutorial we have concentrated on data binding, including linking controls to each other and to other data sources. In this article we will expand upon this information by looking at priority bindings.

A priority binding includes several data binding sources. As the name suggests, these sources are prioritised. If the first possible binding source produces a result that can be applied to the target property, it is used. If not, the second data binding option is attempted. This continues until a valid source is identified or all options have been exhausted.

We can demonstrate the use of priority bindings with a simple example. Create a new WPF application in Visual Studio named, "PriorityBindingDemo". Once the project is ready, replace the XAML in the main window with the following:

<Window x:Class="PriorityBindingDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PriorityBinding Demo" Height="150" Width="200">
    <StackPanel>
        <TextBlock Margin="2"/>
        <Slider Maximum="100" Margin="2" IsEnabled="False"/>
        <TextBlock Margin="2 10 2 2"/>
    </StackPanel>
</Window>

We'll use priority bindings to set the properties of the TextBlocks and the Slider. First we need a data source. Create a new class file named, "DataObject" and set its code as shown below:

public class DataObject
{
    public int NumericValue
    {
        get { return 42; }
    }

    public string StringValue
    {
        get { return "Forty-two"; }
    }
}

Finally, modify the constructor of the MainWindow class to set the DataContext for the window:

public MainWindow()
{
    InitializeComponent();
    DataContext = new DataObject();
}

Adding a PriorityBinding

Priority bindings can be created in XAML using a PriorityBinding element. This is generally applied to a control's property using property element syntax. The PriorityBinding element contains multiple Bindings, each providing a potential source for the value.

For example, the following PriorityBinding tries to apply the value from StringValue. If this cannot be obtained, or is incompatible with the target property, NumericValue is interrogated instead.

<PriorityBinding>
    <Binding Path="StringValue" />
    <Binding Path="NumericValue" />
</PriorityBinding>

Let's apply this binding to the first TextBlock and to the Slider. Replace the XAML for those two controls with the following code:

<TextBlock Margin="2">
    <TextBlock.Text>
        <PriorityBinding>
            <Binding Path="StringValue" />
            <Binding Path="NumericValue" />
        </PriorityBinding>
        </TextBlock.Text>
</TextBlock>
<Slider Maximum="100" Margin="2" IsEnabled="False">
    <Slider.Value>
        <PriorityBinding>
            <Binding Path="StringValue" Mode="OneTime" />
            <Binding Path="NumericValue" Mode="OneTime" />
        </PriorityBinding>
    </Slider.Value>
</Slider>

Using this code, both controls are bound to the same two possible source values. In the case of the TextBlock, the StringValue property returns a compatible value. The TextBlock therefore shows the text, "Forty-two". The Slider's Value cannot be bound to a string so the second source property, "NumericValue", is used. This is suitable for the property so the slider shows the value, 42. This moves the slider's thumb to just left of centre.

WPF PriorityBinding demonstration window

30 September 2014