C# - How to determine whether a property is changed
I need to know whether a p开发者_Python百科ublic 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 BardaJust 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 should work but you could also just create your own event that is specific to the property without dragging in system.componentmodel.
精彩评论