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.

Asynchronous Priority Bindings

One of the more common uses of priority bindings is with asynchronous data binding, which we saw in the previous article. If you use multiple, asynchronous bindings within a PriorityBinding element, the properties can be read concurrently. As each property returns a value, the whole binding is reconsidered and the displayed values may change.

This type of priority binding is useful for properties that take a long time to return a value, or where the value can come from multiple sources but you don't know which will be the quickest. You can also take advantage of priority bindings to show updates as a property is prepared.

For example consider the following binding:

<PriorityBinding>
    <Binding Path="Slow" IsAsync="True" />
    <Binding Path="Medium" IsAsync="True" />
    <Binding Path="Fast" IsAsync="True" />
</PriorityBinding>

Here we have three possible sources for our data, each that expected to be ready at a different time. If the Fast value is ready and compatible with our target property, whilst the Slow and Medium values are not, the value from Fast is displayed. When the Medium property is ready, it will override the information in Fast, updating the control. When the Slow value is ready, it will be displayed. If the properties are readied in the expected order, all three values will be displayed in turn.

Let's apply this binding to the lower of the TextBlocks. Replace the TextBlock with the following code:

<TextBlock Margin="2 10 2 2">
    <TextBlock.Text>
        <PriorityBinding FallbackValue="Please wait...">
            <Binding Path="Slow" IsAsync="True" />
            <Binding Path="Medium" IsAsync="True" />
            <Binding Path="Fast" IsAsync="True" />
        </PriorityBinding>
    </TextBlock.Text>
</TextBlock>

We need to add the three properties to the DataObject class and introduce a delay that emulates long-running processes. We'll use the Thread.Sleep method to add the pauses, so include the following using directive in the class.

using System.Threading;

Add the following properties to the DataObject class:

public string Slow
{
    get
    {
        Thread.Sleep(10000);
        return "All done";
    }
}

public string Medium
{
    get
    {
        Thread.Sleep(7000);
        return "Nearly there";
    }
}

public string Fast
{
    get
    {
        Thread.Sleep(4000);
        return "Getting ready";
    }
}

Run the program to see the results. Initially the text block will be empty. After a brief pause the text will change to "Getting ready", followed by "Nearly there" and "All done".

Fallback Values

PriorityBinding includes a FallbackValue property, similar to other data binding declarations. This allows you to specify a value to display when none of the bindings are able to provide a suitable value for the target. You can use this with asynchronous bindings to set a value to use until the first property is read.

Update the data binding for the last text block as follows, then run the program for a final time. You should see that the control now shows "Please wait..." until the first property has successfully returned a value.

<PriorityBinding FallbackValue="Please wait...">
    <Binding Path="Slow" IsAsync="True" />
    <Binding Path="Medium" IsAsync="True" />
    <Binding Path="Fast" IsAsync="True" />
</PriorityBinding>
30 September 2014