On the odd occasion, we may need to specify a number of source binding paths and want to map them to a single binding target property. One way that we can do this is to use the MultiBinding class and we'll see an example of this in the last section of this chapter. However, there is an alternative class that we can use that provides us with some additional functionality.
The PriorityBinding class enables us to specify multiple bindings and gives each a priority, with the bindings that are declared first having the highest priority. The special functionality of this class is that it will display the value from the first binding that returns a valid value and if that is not the binding with the highest priority, it will then update the display with the value from the highest priority binding when it is successfully resolved.
To clarify further, this enables us to specify a binding to a normal property that will resolve immediately, while the actual value that we want to data bind to is being downloaded, calculated, or otherwise being resolved over time. This enables us to supply a default image source while the actual required image is being downloaded, or to output a message until a calculated value is ready for display. Let's look at a simple XAML example:
<TextBlock> <TextBlock.Text> <PriorityBinding> <Binding Path="SlowString" IsAsync="True" /> <Binding Path="FastString" Mode="OneWay" /> </PriorityBinding> </TextBlock.Text> </TextBlock>
In the preceding example, we set the PriorityBinding on the TextBlock.Text property and inside, specify two bindings. The first has the higher priority and has the actual property value that we want to display. Note that we set the IsAsync property to True, to specify that this binding will take some time to resolve and that it should not block the UI thread.
The second binding is data bound to a normal property using a One-Way binding that simply outputs a message:
public string FastString { get { return "The value is being calculated..."; } }
By using the PriorityBinding element, this message will be output instantly and then updated with the actual value from the SlowString property when it is ready. Let's now move on and investigate one further type of Binding class.