未调用依赖属性的 PropertyChangedCallback [英] Dependency Properties' PropertyChangedCallback not getting called

查看:30
本文介绍了未调用依赖属性的 PropertyChangedCallback的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有自己的用户控件,带有 DependencyProperty 和相应的回调方法,如下所示:

If have an own user control with a DependencyProperty and the corresponding callback method like below:

public partial class PieChart : UserControl
{
    public static readonly DependencyProperty RatesProperty = DependencyProperty.Register("Rates", typeof(ObservableCollection<double>), typeof(PieChart),
        new PropertyMetadata(new ObservableCollection<double>() { 1.0, 1.0 }, new PropertyChangedCallback(OnRatesChanged)));

[...]

    public static void OnRatesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((PieChart)d).drawChart();
    }

使用此用户控件时,我将名为Rates"的 ObservableCollection 绑定到 RatesProperty.费率和相关方法如下所示:

When using this user control, I bind an ObservableCollection called "Rates" to the RatesProperty. Rates and the related methods look like this:

private ObservableCollection<double> rates;
public ObservableCollection<double> Rates
{
    get { return this.rates; }
    set
    {
        if (this.rates != value)
        {
            this.rates = value;
            OnPropertyChanged("Rates");
        }
    }
}

public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

当我更改 ObservableCollection Rates 时(例如,使用 this.Rates = new ObservableCollection<double>() { 1.0, 2.0 })用户控件的 OnRatesChanged() 方法按预期调用.但是当我执行以下操作时,它不会被调用:

When I change the ObservableCollection Rates (e.g. with this.Rates = new ObservableCollection<double>() { 1.0, 2.0 }) the OnRatesChanged() method of the user-control is called like expected. But when I execute the following, it is not called:

this.Rates[0] = (double)1;
this.Rates[1] = (double)2;
OnPropertyChanged("Rates");

我希望当我使用正确的属性名称引发 PropertyChanged 事件时,始终会调用用户控件中的相应回调.那是错的吗?我找到了这个线程:始终获取依赖属性的 PropertyChangedCallback - Silverlight其中涵盖了 Silverlight,但我认为 WPF 也是如此.

I expected that when I raise the PropertyChanged event with the correct property name, the corresponding callback in the user control is always called. Is that wrong? I found this thread: Getting PropertyChangedCallback of a dependency property always - Silverlight which covers silverlight but I think then that the same is true in WPF.

所以后台的框架会检查绑定的属性(在我的示例中为Rates")是否发生了变化,并且只有当它发生变化时,它才会调用关联的回调,对吗?因此更改我的集合的元素没有任何效果,我总是必须更改完整的集合?

So the Framework in the background checks if the bound property (in my example "Rates") changed and only if it changed, it calls the associated callback, correct? Thus changing the elements of my collection has no effect, I always have to change the complete collection?

谢谢!

推荐答案

你的结论是对的,你的 OnRatesChanged 回调只有在 Rates 依赖属性被设置时才会被调用到一个新的集合(或 null).

Your conclusion is right, your OnRatesChanged callback will only be called when the Rates dependency property is set to a new collection (or null).

为了获得有关集合更改的通知,您还必须注册一个 NotifyCollectionChangedEventHandler:

In order to get notified about changes in the collection, you would also have to register a NotifyCollectionChangedEventHandler:

private static void OnRatesChanged(
    DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var pieChart = (PieChart)d;
    var oldRates = e.OldValue as INotifyCollectionChanged;
    var newRates = e.NewValue as INotifyCollectionChanged;

    if (oldRates != null)
    {
        oldRates.CollectionChanged -= pieChart.OnRatesCollectionChanged;
    }

    if (newRates != null)
    {
        newRates.CollectionChanged += pieChart.OnRatesCollectionChanged;
    }

    pieChart.drawChart();
}

private void OnRatesCollectionChanged(
    object sender, NotifyCollectionChangedEventArgs e)
{
    switch (e.Action)
    {
        ...
    }

    drawChart();
}

这篇关于未调用依赖属性的 PropertyChangedCallback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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