Hi!
I have the problem:
I have some controls on a form (a check-box, a combo-box, a slider, a text-box). Their values are bound to different properties of the view model.
When a property of the view model has a certain value, I
want these control to be "fixed" (a error message is displayed and they are set to some fixed value (e.g: the check-box is unchecked when the user tries to check-it, the slider is set to a certain value, the combo's selected item is the second item in the list).
I did this this way (a simplified example for text-box):
In the view:
<TextBox
Text="{Binding ViewModelProperty,
NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged,
ValidatesOnDataErrors=True, NotifyOnValidationError=True}"
/>
In the view model: The property is defined like this:
String _ViewModelProperty;
public String ViewModelProperty
{
get
{
return _ViewModelProperty;
}
set
{
_ViewModelProperty = value;
OnPropertyChanged("ViewModelProperty");
}
}
and the implementation of IDataErrorInfo:
String IDataErrorInfo.this[String propertyName]
{
get
{
String error = null;
if (propertyName == "ViewModelProperty")
{
if (ViewModelProperty != "FixedValue")
{
error = DisplayMessage("You can only set a fixed value here");
ViewModelProperty= "FixedValue";
}
}
return error;
}
}
This works well for the check-box, but for all other controls, it functions like this: the user sets the "wrong" value, the error message is displayed and then, instead of updating the control with the fixed value, the wrong value is still displayed (it is not synchronized with the view model anymore).
I can't figure out how to force an update of the control's value.
Thank you in advance.