Priority Binding in WPF

Priority binding is one of awesome features in WPF. It works by specifying a list of bindings. The list of bindings is ordered from the highest priority to the lowest priority. If the highest priority binding returns a value of successful 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.

It can be used to optimize a WPF application's performance as well. When we need to bind a large list of data some properties in that list are taking time because of some heavy calculation then until that time we can bind some dummy data, say in case of string “…” or in case of int empty or zero value then as soon as the value is calculated or processed it will automatically be changed to the highest priority binding. You can improve your loading time and scrolling (if you have used virtualization and a long list of data is there).

In the following example we are trying to bind a text block with one text that will return the value in 6 seconds. Until then we are binding that TextBlock to some dummy value say “Empty Value” . After 6 seconds the value is processed and based on a higher priority it will automatically be updated.

XAML Code

XAML Code

CS Code

CS Code

So when we are binding large data, say we are binding a listbox to a list of students having 60 properties and we have created some control template for the list box then at that time some properties do not take much time but some take time for the calculation and processing. Until that time we can use priority binding for those properties and we can bind them with a null value so the loading in terms of the user visual is faster but some data is still in progress.

I hope this article will help you in performance improvement and learning Priority Binding.