Dependency Property Precedence: CodeBehind vs. XAML
When I initialize a control property from code, binding to the same property defined in XAML doesn't work. Why?
For example, I set control properties on startup using these statements:
myControl.SetValue(UIElement.VisibilityProperty, DefaultProp.Visibility);
myControl.SetValue(UIElement.IsEnabledProperty, DefaultProp.IsEnabled);
and on xaml I bind the myControl property like this:
IsEnabled="{Binding Path=IsKeyControlEnabled}"
now when the IsKeyControlEnabled property is changed to false, myControl remains enabled (because it is initialized to true).
How can i do this?
+2
a source to share
1 answer
This is the correct behavior - it is by design. Explicitly assigned values override values obtained using data bindings. WPF bindings eliminate the need to explicitly reference user interface objects and their properties. To set the value of a property, just change the value it is bound - in your case:
IsKeyControlEnabled = DefaultProp.IsEnabled;
+3
a source to share