views:

240

answers:

1

When I initialize a control property from code, the binding to the same property defined on XAML don't work. Why?

For Example, I set control properties on startup with this statements:

myControl.SetValue(UIElement.VisibilityProperty, DefaultProp.Visibility);
myControl.SetValue(UIElement.IsEnabledProperty, DefaultProp.IsEnabled);

and on xaml I bind the property of myControl in this way:

 IsEnabled="{Binding Path=IsKeyControlEnabled}"

now, when the property "IsKeyControlEnabled" changes to false, myControl remains enabled (because it's initialize with true value).

How can I do?

+3  A: 

This is the proper behavior - it is by design. Explicitly assigned values override values obtained through data bindings. WPF bindings remove the need to explicitly reference UI objects and their properties. To set the value of the property simply change the value it is binded to - in your case:

IsKeyControlEnabled = DefaultProp.IsEnabled;
Aviad P.
Is not there another way? because the class that set de default values is an external and generic class that I used to initialize the several controls of window. There is a function (with a type Control input parameter) that define some settings for the specific control.While the property "IsKeyControlEnabled" is manage on window.
LukePet
Why are you looking for another way? `IsKeyControlEnabled` holds the value anyway, because of the binding...
Aviad P.
This is by design, setting local values will overwrite the binding(s), so no, there is no "other way", as the framework is designed this way
miguel