当新值相同时,DependencyProperty 不会触发 ValueChanged [英] DependencyProperty doesn't fire ValueChanged when new value is the same

查看:29
本文介绍了当新值相同时,DependencyProperty 不会触发 ValueChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,问题来了:我写了一个 UserControl,它接收一个新值,比如每 100 毫秒,然后用它做一些事情.它必须处理每个新的值设置器,即使值没有改变.UserControl 有一些 DependencyProperties:

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));

在使用此控件的 XAML 中,我只是将 CurrentValue 的 Binding 设置为 (INotifyPropertyChanged-enabled) 属性:

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}" ... />

视图模型:

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

如您所见,我明确注释掉了 if equals then return,因此即使该值更新为相同的值,它也会触发 PropertyChanged 事件.

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.

现在回到我的用户控件,我尝试通过两种方式注册 ValueChanged;首先使用 DependencyPropertyDescriptor:

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);

或使用UIPropertyMetaData:

new UIPropertyMetadata(0d, OnCurrentValueChangedCallback)

所以回调的存根看起来像:

so a stub of the callback would look like:

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

好的,现在的问题是,当值没有显式改变时不会触发回调.我检查了一下,PropertyChanged 事件在我的视图模型中触发,但控件没有看到更改.当值更改为新值时,它将按预期处理回调.
有什么方法可以覆盖这种行为,以便我的回调方法总是被命中?

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?


我也尝试使用 CoerceValueCallback,当值保持不变时,它会被击中,但我认为它对我的问题没有帮助......


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...

推荐答案

您可以将您的值包装在一个对象中,即创建一个类来保存它 - 然后将该属性设置为包含新值的该类的新实例, 每次.这意味着您每秒创建约 10 个对象,但它们各不相同,会触发更改事件,并且很小(无论如何都会被 GC 处理).:)

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). :)

这篇关于当新值相同时,DependencyProperty 不会触发 ValueChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆