冒泡 INotifyPropertyChanged 和嵌套属性
如果我有以下布局:
public class A : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public B { get; set; }
}
public class B { public C { get; set; } }
public class C { public D { get; set; } }
public class D { public E { get; set; } }
//... add n classes
public class Z
{
public int Property
{
set
{
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Property"));
}
}
}
当 ABCDE..Z.Property 更改时,我通知 A 的最简洁方法是什么?
当 A 内部的任何内容发生更改时,我希望将其标记为“脏”,以便我可以告诉系统 A 需要保存。
If I have the following layout:
public class A : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public B { get; set; }
}
public class B { public C { get; set; } }
public class C { public D { get; set; } }
public class D { public E { get; set; } }
//... add n classes
public class Z
{
public int Property
{
set
{
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Property"));
}
}
}
What is the cleanest way for me to notify A when A.B.C.D.E...Z.Property changes?
When anything inside of A changes, I want it to be flagged as "dirty" so I can tell the system that A needs to be saved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事实上,我最近正在研究这个完全相同的问题。我的做法是简单地让 B、C、D 等管理自己的 Dirty 状态,然后修改 A 的 IsDirty 属性,如下所示:
对我来说,这不仅简单,而且使最有意义。如果 A 的任何属性发生了变化,则 A 是脏的,而 B、C、D 等都是 A 的属性。
I was actually working on this exact same problem just recently. My approach was to simply let B, C, D, etc. to manage their own Dirty state, and then modified A's
IsDirty
property as such:To me, this is not only simple, but it makes the most sense. A is dirty if any of it's properties have changed, and B, C, D, etc are all properties of A.
我没有测试它,但下面的一个应该可以工作。我不记得为什么,但我认为你无法处理 PropertyChanged 事件。您应该声明自己的委托(VoidHandler)。
I didn't test it, but following one should work. I don't remember why, but I think you cannot handle PropertyChanged events. You should declare your own delegate (VoidHandler).
对于具有公共基类的业务应用程序,我按照
实现 INotifyPropertyChanged 执行 此操作- 是否存在更好的方法?
进行一些修改来检查“冒泡”属性。
基类
子类
提供
性能
这种方法会带来相关的性能影响...比仅使用字符串慢大约 20%。也就是说,虽然指标和跟踪显示它速度较慢,但我实际上无法分辨出差异,因此这次打击是值得的:我参与开发的各种应用程序的应用程序维护。
替代实现
为了获得更好的性能,您可以使用两种不同的 SetField 方法;第一个 SetNotifyField 将处理本身实现 INotifyPropertyChanged (如上所述)的属性,第二个 SetField 将处理简单属性。即剪掉
if (field is INotifyPropertyChanged)...
For line of business application that have a common base class I do this as per
Implementing INotifyPropertyChanged - does a better way exist?
with some modifications to check for "bubbling" properties.
Base Class
Sub classes
Provides
Performance
There is an associated performance hit to this approach... about 20% slower than just using a string. That said although the metrics and tracing say it's slower I cant actually tell the difference so the hit is worth it re: application maintenance for the kinds of apps I'm involved in developing.
Alternative Implementations
For better performance you could have two different SetField methods; the first SetNotifyField would deal with properties which themselves implement INotifyPropertyChanged (as above) and the second SetField would deal with simple properties. i.e. cut out the
if (field is INotifyPropertyChanged)...