WPF通知的PropertyChanged为获取属性 [英] WPF Notify PropertyChanged for a Get Property

查看:1478
本文介绍了WPF通知的PropertyChanged为获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 INotifyPropertyChanged的使用 CallerMemberName



<$实现的p $ p> 公共事件PropertyChangedEventHandler的PropertyChanged;

受保护的虚拟无效OnPropertyChanged([CallerMemberName]字符串参数propertyName = NULL)
{
如果(的PropertyChanged!= NULL)
{
的PropertyChanged(这一点,新PropertyChangedEventArgs(propertyName的));
}
}



所以这可能在任何setter方法被调用因为 - OnPropertyChanged()时,它被设置这将通知属性更改事件。这不是一个属性仅一个吸气的情况。例如,

 私人的DateTime _dob; 
公众的DateTime DATEOFBIRTH
{
得到
{
返回_dob;
}
私定
{
_dob =价值;
OnPropertyChanged();
OnPropertyChanged(时代);
}
}

公众诠释年龄
{
得到
{
返回DateTime.Today.Year - _dob.Year ;
}
}



OnPropertyChanged()为DATEOFBIRTH工作正常,但通知时代变了,我要记得叫 OnPropertyChanged(时代) DATEOFBIRTH 。我感觉这使得代码难以维持一段时间。如果一个新的属性取决于年龄,这也需要DATEOFBIRTH的制定者得到通知。有没有更好的办法做到这一点,而无需调用OnPropertyChanged(时代)?


解决方案

只要你的依赖属性是在同一个类可以使用波马公司的做法,但如果依赖的属性在不同的班级其掌握这种方法更难了。



在我看来做概念上正确的做法是要添加一个PropertyChanged的听众



在这种情况下,这将是类似



在构造函数中:

  this.PropertyChanged + =新PropertyChangedEventHandler(SubPropertyChanged); 

和外:

 私人无效SubPropertyChanged(对象发件人,PropertyChangedEventArgs E)
{
如果(e.PropertyName ==出生日期)
{
OnPropertyChanged(时代 );
}
}

这则同样适用,如果你有一个依赖房地产的地方完全不同的,如果你还不能改变你的源类了。


I have the INotifyPropertyChanged implemented using CallerMemberName

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
 if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

So this could be called in the setter of any property as - OnPropertyChanged() which would notify property changed event whenever it is being set. This is not the case for a property a getter only. For example,

private DateTime _dob;
public DateTime DateOfBirth
{
    get
    {
        return _dob;
    }
    private set
    {
        _dob = value;
        OnPropertyChanged();
        OnPropertyChanged("Age");
    }
}

public int Age
{
    get
    {
        return DateTime.Today.Year - _dob.Year;
    }
}

OnPropertyChanged() works fine for DateOfBirth, but to notify Age changed, I should remember to call OnPropertyChanged("Age") within the setter of DateOfBirth. I feel this makes the code difficult to maintain over time. If a new property depends on Age, that also needs to be Notified in the setter of DateOfBirth. Is there a better way to do this without calling OnPropertyChanged("Age")?

解决方案

As long as your dependent property is in the same class you can use Poma's approach but if the dependent properties are in different classes its getting harder with that approach.

In my opinion the conceptually right thing to do would be to add a PropertyChanged listener.

In this case that would be something like

In the constructor:

this.PropertyChanged += new PropertyChangedEventHandler(SubPropertyChanged);

And outside:

private void SubPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "DateOfBirth")
    {
       OnPropertyChanged("Age");
    }
}

This then also works if you have a dependent property somewhere completely different and also if you cannot change your source class anymore.

这篇关于WPF通知的PropertyChanged为获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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