Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've noticed that when doing dependency properties if the value is already been set during the definition of the property it won't act correctly. You have said that there were no problems when setting the value within the application, so I'm going to assume there is no plumbing problems.</p> <p>Consider the example below:</p> <p><strong>XAML</strong></p> <pre><code>&lt;UI:PriorityControl Priority="{Binding Path=Priority, Mode=TwoWay}"/&gt; </code></pre> <p><strong>Code</strong></p> <pre><code>public enum Priority { Normal, Low, High } public class PriorityControl : UserControl { public static readonly DependencyProperty PriorityProperty = DependencyProperty.Register("Priority", typeof(Priority), typeof(PriorityControl), new PropertyMetadata(DependencyProperty.UnsetValue, OnPriorityPropertyChanged); public Priority Priority { get { return (Priority)this.GetValue(PriorityProperty); } set { this.SetValue(PriorityProperty, value); } } private static void OnPriorityPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { PriorityControl control = sender as PriorityControl; if (control != null) { // Do what you wanted to here. } } } </code></pre> <p>If you're using an enum, and not specifying a default value (which you appear not to be by not providing a PropertyMetadata object to the dependency property on creation) the default value would be that of the enum's default value. In my above example that would be "Normal". Now if you tried to set the value to Normal on creating the control, the callback would not be called because the value already equals that value.</p> <p>In order to allow this to work correctly, you must pass the DependencyProperty.UnsetValue to the PropertyMetadata object as I have done when I defined the property. That will allow the OnPriorityPropertyChanged method to be called when the value is being set initially.</p> <p>Hope that helps fix it!</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