(I know this is old but I ran into this problem today so I was forced to do some investigation)
It appears that binding occurs in the order the DependencyProperties are defined.
For instance, in MyControl.cs
public int Mode { get => (int)GetValue(ModeProperty); set => SetValue(ModeProperty, value); }
public static readonly DependencyProperty ModeProperty
= DependencyProperty.Register(nameof(Mode), typeof(int), typeof(MyControl),
new FrameworkPropertyMetadata(new PropertyChangedCallback(Mode_Changed)));
public string Result { get => (string)GetValue(ResultProperty); set => SetValue(ResultProperty, value); }
public static readonly DependencyProperty ResultProperty
= DependencyProperty.Register(nameof(Result), typeof(string), typeof(MyControl),
new FrameworkPropertyMetadata(new PropertyChangedCallback(Result_Changed)));
private static void Mode_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
//Mode changed. Update display.
}
private static void Result_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
//Result changed. Update display.
}
And the XAML:
<controls:MyControl Result="You passed!"
Mode="3" />
Because Mode
is defined as a dependency property before Result
, Mode
will be set first. The order in which you assign the values in the XAML is ignored.
So in theory, to answer your question, make sure the Mode property is defined first. This solved my issue.