In my previous blog post I said that we’d cover binding priorities, so here we go. In WPF there is something called Dependency Property Value Precedence but it’s mostly hidden behind the scenes. However, Perspex has a CSS-style styling system which means that the priorites (or precedence) of bindings become even more important.
To illustrate, lets consider this simple Button template:
The style snippet here only covers setting the background color of a Button, but what it tells us is:
- All buttons have a default background color
- The background color is overridable by setting Button.Background
- When the button is pressed, it should be displayed with a “pressed” background.
So we have 3 levels of binding, in increasing precedence:
- The color set in the base style.
- The color set on the template parent.
- The color set when the
:pressedclass is present on the Button.
And indeed if you look at the
BindingPriority enumeration you’ll see something very much like that:
This is implemented in Perspex using the
PriorityValue class. A
PriorityValue object is created for each property in a
PerspexObject that has a value set.
In the original implementation
PriorityValue this was implemented using a linked list of bindings. When a new binding was added to a property, it was added into the linked list before any other bindings with the same or higher BindingPriority value. The active binding was simply the first binding in the list that didn’t return
PerpsexProperty.UnsetValue. Values that were set using
PerspexObject.SetValue were simply bindings that produced a single value.
However you’ll notice I’m speaking in the past tense - this system works well enough for the styling example described at the start of the article, but after a while it became clear that it wasn’t enough.
We’ll get into that in the next installment.