This project is read-only.
1
Vote

Dial90 binding stop working

description

I am using Dial90NorthWest.
When Value greater than Maximum or lower than Minimum, all binding stop working. I should reopen the page to get it works

comments

Replika wrote Sep 24, 2010 at 9:38 AM

First looking in your Dashboard.cs, I think you should not put any validating data in PropertyChangedCallback, use CoerceValueCallback instead.

public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(double), typeof(Dashboard), new PropertyMetadata(0.0, new PropertyChangedCallback(ValuePropertyChanged), new CoerceValueCallback(CoerceValueProperty)));
(...)

private static object CoerceValueProperty(DependencyObject dependancy, object value)
{
Dashboard instance = dependancy as Dashboard;
if (instance != null && instance.DashboardLoaded) {
    if ((double)value > instance.RealMaximum) {
        value = instance.RealMaximum;
    }
    if ((double)value < instance.RealMinimum) {
        value = instance.RealMinimum;
    }
}
return value;
}

odahan wrote Oct 28, 2010 at 3:47 PM

You're right. There must be no coerce in the value properrty changed event and CoerceValueCallback is the right place to do it : but you're missing that this lib must work under WPF and Silverlight and the latter does not provide the Coerce callback (unfortunately).
So the only place to coerce is in the value changed event, but this is leading to another problem : since a binding is seen as a "local value", when coercing, so setting a new value by code, the latter is setting a new local value. This new value erases all other local values, so the binding. And once lost, the only way to restablish it is to recreate the binding object.
The main problem is then : how to coerce under silverlight. The answer is : you can't (until SL Team give us back the wpf Coerce callback).
I saw some very sophisticated code trying to implement coerce in silverlight, managing recursive calls with different levels etc... I was really affraid to implement such kind of code... There are some samples in the framework itself (just use reflector to get the C# from Slider object form exemple). This is really horrible. And they did it on one or two properties of a few components, we can't use such code with dozen of properties in a lot of objects.
So, the person who wrote the gauge components can't use the coerce callback because SL does not implement it. But the way he used to solve the problem is to simple and break any binding.
The solution is simply to supress the validation code from the gauge... In a well done WPF/SL app, you must use Prism or MVVM pattern, and validatiing and coercing values can be (must be?) the job of the ViewModel. Interface object must not validate anything. It sounds absurd, it is an object programming anti-pattern, but since SL will not offer the coerce callback there is no real other way...

wrote Feb 14, 2013 at 9:10 PM