简体   繁体   中英

C# - How to determine whether a property is changed

I need to know whether a public property (which has getter & setter) is changed. The property is in a simple class (no user control/component etc).
Is there an elegant way to subscribe to some kind of event which notifies when property is changed?
I tried to see what microsoft is doing in their Binding object (using reflector) and that lead me to explore the PropertyDescriptor.AddValueChanged method but it didn't worked for me. maybe it works only for components/user controls...

Any suggestions?

Thanks,
Adi Barda

Just implement the INotifyPropertyChanged interface:
http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

This is a pretty well known interface, which is used by the binding APIs. Just follow the example implementation on that msdn page.

INotifyPropertyChanged应该可以工作,但您也可以创建自己的特定于该属性的事件,而无需在system.componentmodel中拖动。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM