Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DependencyProperty doesn't fire ValueChanged when new value is the same

Ok so here's the problem: I wrote a UserControl which receives a new value say like every 100ms and does something with it. It has to handle each new value setter, even if the value didn't change. The UserControl has a few DependencyProperties:

public double CurrentValue
    {
        get { return (double)GetValue(CurrentValueProperty); }
        set { SetValue(CurrentValueProperty, value); }
    }

    public static readonly DependencyProperty CurrentValueProperty =
       DependencyProperty.Register("CurrentValue", typeof(double), typeof(GraphControl), new UIPropertyMetadata(0d));

In the XAML where this control is used, I just set the Binding of CurrentValue to a (INotifyPropertyChanged-enabled) property:

<uc:UserControl CurrentValue="{Binding MyValue}" ... />

viewmodel:

    public double MyValue
    {
        get { return _value; }
        set
        {
            //if (_value == value) return;
            _value= value;
            if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs("MyValue"));
        }
    }

As you can see, I explicitly commented out the if equals then return so it will fire the PropertyChanged event even when the value gets updated to the same value.

Now back to my user control, I tried registering the ValueChanged in two ways; first by using the DependencyPropertyDescriptor:

var propertyDescriptor = DependencyPropertyDescriptor.FromProperty(CurrentValueProperty, typeof(GraphControl));
propertyDescriptor.AddValueChanged(this, OnCurrentValueChanged);

or by using the UIPropertyMetaData:

new UIPropertyMetadata(0d, OnCurrentValueChangedCallback)

so a stub of the callback would look like:

private void Callback(object sender, EventArgs e){
    //do stuff
}

Ok now the problem is, the callback is not fired when the value doesn't explicitly change. I checked, and the PropertyChanged event is firing in my viewmodel, but the control doesn't see the change. When the value changes to a new value, it will handle the callback as expected.
Is there any way to override this behavior so that my callback method will always get hit?

EDIT:
I also tried using the CoerceValueCallback, and that one is hit when the value stays the same, but it doesn't help me with my problem I think...

like image 803
RoelF Avatar asked Jul 26 '11 07:07

RoelF


1 Answers

You can wrap your value up in an object, i.e. create a class to hold it - then set the property to a new instance of that class containing the new value, every time. That means you're creating ~10 objects per second, but they are each different, will trigger the change event, and are only small (will be GC'd anyway). :)

like image 199
Kieren Johnstone Avatar answered Sep 26 '22 02:09

Kieren Johnstone