Dependency Property Set Priority: CodeBehind vs. XAML - wpf

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?

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;

Related

Binding Slider.Value to property and setting Value to value above Slider.Maximum will coerce it to Maximum but the ViewModel will go out of sync

Before I start to explain my problem: The Slider is just an example for the following problem. I work on a custom control, that has to do a very similar thing like the slider does here:
Scenario:
I have bound a Slider.Value to my ViewModel property called MyValue.
I have defined the Maximum of the Slider to be 100.
Now I set the Property MyValue to 200.
What is the problem?
The Slider will internally coerce the value to be 100.
But the property MyProperty isn't being updated.
In my case (the custom control I built), I need to continue with the coersed value (100) later on, NOT the old (200) value.
Question:
Is there anything I can do in my custom control?
For example I tried myControl.GetExpression(MyValueProperty).UpdateSource(); in both my CoerseValueCallback and PropertyChangedCallback, but no matter what I try, the setter of the bound ViewModel-property is NEVER called and the property stays out of sync.
Bind the Maximum property of your Slider to another view model property and implement logic to ensure that your values are always within the valid range in your view model class.
The control rightfully coerces the values but the logic of synchronizing them should be implemented in the view model class.
Your custom control should be able to simply set the source property of its current DataContext whenever the value is coerced though.
this.GetBindingExpression(Slider.ValueProperty).ResolvedSourcePropertyName should give you the name of the source property and then you can for example set it using reflection if calling UpdateSource() on the BindingExpression doesn't work.

How to hide a control when it has an invalid binding

I would like to hide a control such as a TextBox when it has a binding on a property such as the Text property that is invalid (has thrown a BindingExpression path error). This is different from simply checking to see if the binding's value is null in a trigger. Null may in fact be a perfectly acceptable value, and the control should still be displayed. Basically I have a DataTemplate that I want to reuse with several different types of objects from my VM, some of which may have certain properties in common, and some of them may have unique properties. When a particular property does not exist on the object that is currently the binding source of the DataTemplate, the DataTemplate will have some sort of additional trigger or binding that will hide the particular control that is bound to the property that does not exist.
You could play around with the Validation.ErrorTemplate to style the control when a validation error occurs.
Then just set the ValidatesOnDataError = True in the binding and away you go

How to create a dependency property in Silverlight using MVVM?

let say there is a textbox and i want to control the visibility of this control using MVVM, is there a sample on how to do this? First create a dependency property then get it hooked up in the ViewModel. Thanks.
Typically, you wouldn't need to use a dependency property in this case. Dependency properties really only need to be implemented for things like controls themselves, not for determining behavior. Behavior, such as the visibility of an element, can be handled directly via data binding.
Your ViewModel would just have some property, and you'd bind the TextBox.Visibility property directly to the ViewModel property.
The one "sticky point" is that you often will want to have some type of IValueConverter that will convert from your property type to a Visibility enum.

Binding an Element to a Control Property (string)

so, i've found a way to bind a label to a property on current Control
i give it a name:
<UserControl x:Class="WpfGridtest.GridControl" x:Name="GridControlControl1">
and than bind to property of this control:
<Label Content="{Binding ElementName=GridControlControl1, Path=Filter}"></Label>
I can see the default value i put in that property.
I am guessing that this isn't working because i am binding to String property which doesn't implement INotifyPropertyChanged??
is there some other type i should be using for this property instead of String auto notify my label of changes, or am i going about this the wrong way?
The INotifyPropertyChanged interface should be implemented by the class that contains the property - in this case, by your WpfGridtest.GridControl.
Also, if you want to use your properties for UI, consider using a DependencyProperty as a storage instead of a private field.
in addition, it is also likely that the default binding mode is one time, so you may have to change it in your {Binding}

BindableAttribute, Combobox, Selectedvalue property - WinForms

I am deriving from combobox (WinForms) and am providing a new implementation for the Selectedvalue property.
It works fine as is, but any change to the selectedvalue property is not updating other controls bound to the same "binding context" to change their values accordingly.
I did try adding the BindableAttribute(true) to the property, but still it does nottrigger the change in value to the other linked controls.
The control's DataBindings.add(...) is all set up. And other controls are also bound to the same data filed on the same datasource.
Any ideas what i am doing wrong.
Have you called your base class' implementation of overridden methods? It's possible that failing to call the base class implementation is accidentally circumventing the code that fires various event plumbing.

Resources