Files
Andy (Steve) De George be103da07e Replace various WPF include files with content (#1335)
* net-current-v30plus-md.md

* net-current-v40plus-md.md

* tla2sharptla-ui-md.md

* tla2sharptla-uiautomation-md.md

* tla2sharptla-winclient-md.md

* tla2sharptla-xaml-md.md

* tlasharptla-ui-md.md

* tlasharptla-uiautomation-md.md

* tlasharptla-winclient-md.md

* tlasharptla-xaml-md.md

* fix warnings
2022-03-16 12:14:11 -04:00

3.7 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Implement PriorityBinding 03/30/2017
csharp
vb
data binding [WPF], PriorityBinding class
d63b65ab-b3e9-4322-9aa8-1450f8d89532

How to: Implement PriorityBinding

xref:System.Windows.Data.PriorityBinding in Windows Presentation Foundation (WPF) works by specifying a list of bindings. The list of bindings is ordered from highest priority to lowest priority. If the highest priority binding returns a value successfully when it is processed then there is never a need to process the other bindings in the list. It could be the case that the highest priority binding takes a long time to be evaluated, the next highest priority that returns a value successfully will be used until a binding of a higher priority returns a value successfully.

Example

To demonstrate how xref:System.Windows.Data.PriorityBinding works, the AsyncDataSource object has been created with the following three properties: FastDP, SlowerDP, and SlowestDP.

The get accessor of FastDP returns the value of the _fastDP data member.

The get accessor of SlowerDP waits for 3 seconds before returning the value of the _slowerDP data member.

The get accessor of SlowestDP waits for 5 seconds before returning the value of the _slowestDP data member.

Note

This example is for demonstration purposes only. The .NET guidelines recommend against defining properties that are orders of magnitude slower than a field set would be. For more information, see Choosing Between Properties and Methods.

[!code-csharpPriorityBinding#1] [!code-vbPriorityBinding#1]

The xref:System.Windows.Controls.TextBlock.Text%2A property binds to the above AsyncDS using xref:System.Windows.Data.PriorityBinding:

[!code-xamlPriorityBinding#2]

When the binding engine processes the xref:System.Windows.Data.Binding objects, it starts with the first xref:System.Windows.Data.Binding, which is bound to the SlowestDP property. When this xref:System.Windows.Data.Binding is processed, it does not return a value successfully because it is sleeping for 5 seconds, so the next xref:System.Windows.Data.Binding element is processed. The next xref:System.Windows.Data.Binding does not return a value successfully because it is sleeping for 3 seconds. The binding engine then moves onto the next xref:System.Windows.Data.Binding element, which is bound to the FastDP property. This xref:System.Windows.Data.Binding returns the value "Fast Value". The xref:System.Windows.Controls.TextBlock now displays the value "Fast Value".

After 3 seconds elapses, the SlowerDP property returns the value "Slower Value". The xref:System.Windows.Controls.TextBlock then displays the value "Slower Value".

After 5 seconds elapses, the SlowestDP property returns the value "Slowest Value". That binding has the highest priority because it is listed first. The xref:System.Windows.Controls.TextBlock now displays the value "Slowest Value".

See xref:System.Windows.Data.PriorityBinding for information about what is considered a successful return value from a binding.

See also