Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The reason why you aren't able to set the value in a <code>Style</code> is because you are setting a local value in your constructor. <a href="http://msdn.microsoft.com/en-us/library/ms743230.aspx" rel="nofollow">MSDN explains <code>DependencyProperty</code> value precedence in more detail</a>.</p> <p>Because you just want to give your property a default value per instance, just use <a href="http://msdn.microsoft.com/en-us/library/system.windows.dependencyobject.setcurrentvalue.aspx" rel="nofollow"><code>SetCurrentValue</code></a> instead of <code>SetValue</code> in your constructor.</p> <p><strong>Edit to explain this further</strong></p> <p>So, a <code>DependencyProperty</code> can be set in multiple ways. It can be set through code, a <code>Binding</code>, a <code>Style</code>, a <code>Trigger</code>, an <code>Animation</code>, or a few other ways. The important thing to know is that there can be multiple attempts to set a given property. </p> <p>Because of this, WPF has defined a precedence for the values. That means if you set a property in a <code>Style</code>, you can set the property manually to override the <code>Style</code> value. Or a <code>Trigger</code> in the <code>ControlTemplate</code> can override the <code>Style</code> value.</p> <p>When you set the property in your constructor, you are giving it a <em>local value</em>. From the first link, you will see that <strong>only</strong> an <code>Animation</code> or <code>Property Coercion</code> can override a local value. </p> <p>The <code>SetCurrentValue</code> method, however, will allow you to set a value for a property <a href="http://msdn.microsoft.com/en-us/library/ms743230.aspx#setcurrentvalue" rel="nofollow"><em>without setting a local value</em></a>. This is what you need, as you want to be able to set the value in a <code>Style</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload