WPF - 为基类实现 System.ComponentModel.INotifyPropertyChanged
我想为基类上的属性实现 System.ComponentModel.INotifyPropertyChanged 接口,但我不太确定如何连接它。
这是我想要获取通知的属性的签名:
public abstract bool HasChanged();
以及我在基类中用于处理更改的代码:
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String info)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(info));
}
}
如何处理基类中事件的连接,而不必在每个子项中调用 OnPropertyChanged()班级?
谢谢,
桑尼
编辑: 好的...所以我认为当 HasChanged() 的值发生变化时,我应该调用 OnPropertyChanged("HasChanged")
,但我不确定如何将其放入基础中班级。有什么想法吗?
I'd like to implent the System.ComponentModel.INotifyPropertyChanged interface for a property on a base class, but I'm not quite sure how to hook it up.
Here's the signature for the property I'd like to get notifications for:
public abstract bool HasChanged();
And my code in the base class for handling the change:
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String info)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(info));
}
}
How do I handle the hookup of the event in the base class without having to call OnPropertyChanged() in each child class?
Thanks,
Sonny
EDIT:
OK... so I think that when the value for HasChanged() changes, I'm supposed to call OnPropertyChanged("HasChanged")
, but I'm not sure how to get that into the base class. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是你所追求的吗?
请注意,OnPropertyChanged 可访问级别受到保护。然后在您的具体类或子类中,您执行以下操作:
编辑:再次阅读OP问题后,我意识到他不想在子类中调用
OnPropertyChanged
,所以我很确定这将起作用:并且在儿童班级中:
Is this what you are after?
Notice the OnPropertyChanged accessible level is protected. And then in your concrete class or child classes, you do:
EDIT: after reading the OP question again, I realize that he does not want to call the
OnPropertyChanged
in the child class, so I am pretty sure this will work:and in child class: