Setters in Style and their precedence

This took a good 30 mins of time before I realised what was going wrong.

I had set a property for a control inline. Then I was trying to change it via EventTrigger and Style. In the style setter, I was reversing the property value i.e. making visibilility from Collapsed to Visible. It never took place. I tried various ways but just couldn’t make it to work.

Finally, realised that the precedence order was the culprit. Styles have a lower precedence when it comes to overriding values. In-line(local) values given within the tag have a higher precedence. Hence, my setter never managed to set the property as it was always overriden by the inline value. Finally, I had to set that inline property as well in the setter and then get the setter to change it again on some event. This worked.

For reference, the following is the precedence order:

  1. Property system coercion. For details on coercion, see Coercion, Animation, and Base Value later in this topic.
  2. Active animations, or animations with a Hold behavior. In order to have any practical effect, an animation of a property must be able to have precedence over the base (unanimated) value, even if that value was set locally. For details, see Coercion, Animation, and Base Value later in this topic.
  3. Local value. A local value might be set through the convenience of the “wrapper” property, which also equates to setting as an attribute or property element in XAML, or by a call to the SetValue API using a property of a specific instance. If you set a local value by using a binding or a resource, these each act in the precedence as if a direct value was set.
  4. TemplatedParent template properties. An element has a TemplatedParent if it was created as part of a template (a ControlTemplate or DataTemplate). For details on when this applies, see TemplatedParent later in this topic. Within the template, the following precedence applies:
    1. Triggers from the TemplatedParent template.
    2. Property sets (typically through XAML attributes) in the TemplatedParent template.
  5. Implicit style. Applies only to the Style property. The Style property is filled by any style resource with a key that matches the type of that element. That style resource must exist either in the page or the application; lookup for an implicit style resource does not proceed into the themes.
  6. Style triggers. The triggers within styles from page or application (these styles might be either explicit or implicit styles, but not from the default styles, which have lower precedence).
  7. Template triggers. Any trigger from a template within a style, or a directly applied template.
  8. Style setters. Values from a Setter within styles from page or application.
  9. Default (theme) style.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s