WPF 为获取属性通知 PropertyChanged
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21776439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Notify PropertyChanged for a Get Property
提问by Carbine
I have the INotifyPropertyChangedimplemented using CallerMemberName
我已经INotifyPropertyChanged使用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,
所以这可以在任何属性的 setter 中调用,因为OnPropertyChanged()它会在设置时通知属性更改事件。对于仅作为 getter 的属性,情况并非如此。例如,
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")?
OnPropertyChanged()DateOfBirth 工作正常,但要通知 Age 更改,我应该记得OnPropertyChanged("Age")在DateOfBirth. 我觉得这使得代码随着时间的推移难以维护。如果一个新的属性依赖于Age,那也需要在DateOfBirth 的setter 中通知。有没有更好的方法来做到这一点而不调用 OnPropertyChanged("Age")?
回答by blacktemplar
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.
只要您的依赖属性在同一个类中,您就可以使用 Poma 的方法,但是如果依赖属性在不同的类中,则使用该方法会变得更加困难。
In my opinion the conceptually right thing to do would be to add a PropertyChanged listener.
在我看来,概念上正确的做法是添加一个 PropertyChanged 侦听器。
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.
如果您在某个地方有一个完全不同的依赖属性,并且您无法再更改源类,那么这也适用。
回答by Poma
One way to do that is to define your attribute and do some reflection in OnPropertyChangedto notify all dependent properties. You may want to cache attributes to use reflection only in class initializer because reflection is pretty slow.
一种方法是定义您的属性并进行一些反射OnPropertyChanged以通知所有相关属性。您可能希望缓存属性以仅在类初始值设定项中使用反射,因为反射非常慢。
private DateTime _dob;
public DateTime DateOfBirth
{
get
{
return _dob;
}
private set
{
_dob = value;
OnPropertyChanged();
}
}
[DependsOnProperty("DateOfBirth")]
public int Age
{
get
{
return DateTime.Today.Year - _dob.Year;
}
}

