My WPF application shows a tree of objects having some properties. These properties can be modified from the UI via data bindings. Objects themselves can be added or deleted. Nothing special.
The question is: What's the best way to be aware of changes somewhere in the objects tree?
Ideas considered so far:
- Create a static method to be called by each object when it is modified
- Create static event on every object type and listen to it
- Implement
INotifyPropertyChanged
on every object and let them all notify their parents about modifications so I can listen to one event on the objects tree root - Listen to events from controls like
TextChanged
,Checked
etc.
They all look like ugly mesh, so I'm afraid to implement anything of t开发者_运维百科hem.
INotifyPropertyChanged is probably the best way to go, as it would allow you to bubble up change notifications from any root node. I guess it would also depend how complex your types are, and what sort of changes you want to react to?
In case, comeone will be ever interested, I ended up subclassing all my objects in the tree from the common ancestor responsible for tracking IsDirty
state and promoting it up and down the tree of objects. I mean that if some object is marked as dirty then all it's parents are considered dirty too. And yes, I implemented INotifyPropertyChanged
in this common superclass to be aware of the changes.
精彩评论