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.