开发者

how do I implement a "Do I Need To Save" mechanism for a data container?

Assume there is the BindingList<Person{Name, age}> class and a PersonManager class that hosts the list. In the application, every control is bount to the person list, in order to manipulate it or getting updated. Eg. A listbox that has 开发者_如何学编程the names of the persons, a control that updates the age for the selected person etc.

I want now to implement a "Do I Need To Save" mechanism for this. Somehow, the ui must know whether the PersonManager has been changed in order to save it.

My though is to catch the INotifyPropertyChanged event that Person implements and create an event in PersonManager that is fired when a propertyChangedEvent is raised. Something like an event forwarding mechanism.

This seems to be error prone. Any suggestions on how to make it better?


If the items in a BindingList implement INotifyPropertyChanged then the BindingList's ListChanged event will fire when one of the items PropertyChanged event fires.

Therefore you only need to handle the BindingList's ListChanged event in your PersonManager and set a dirty boolean flag (dirty being the typical signifier of stale or unsaved data) in your PersonManager. Then, the application can ask the PersonManager if any data is dirty. When the application saves the data, it tells the PersonManager to reset the dirty flag. If you PersonManager also handles the saving of the data then it can reset its own flag.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜