Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ObservableCollection Edit Mode

I am using observable collections all around my applications. My problem is that when i use a popup window for editing those entities, my bound lists are getting changed when the user changes those corresponding fields in the window.

How could i simply freeze the observable changes norifications, and release them only when the entity is saved?

Thanks, Oran

like image 401
OrPaz Avatar asked Oct 25 '10 12:10

OrPaz


3 Answers

I think the issue is not with the collection, but with the entities themselves. ObservableCollection raises an event when an item is added or removed, not when a property of an item is changed. This part is handled by the INotifyPropertyChanged implemented by the item, so it's this notification you need to disable.

I suggest you have a look at the IEditableObject interface, which is designed for this kind of scenario. You can disable the notifications in the BeginEdit method, and reenable them in EndEdit and CancelEdit.


EDIT: Paul Stovell has a nice implementation of an IEditableObject wrapper here : http://www.paulstovell.com/editable-object-adapter

like image 172
Thomas Levesque Avatar answered Oct 24 '22 19:10

Thomas Levesque


You can use:

  BoundPropertyOfViewModel = CollectionViewSource.GetDefaultView(AgentDeploymentDetail);

and bind to the view instead of binding directly to the ObservableCollection. This is the same object that allow you to filter/sort your output without touching the collection.

When you want to stop changes, use DeferRefresh(). When you are done, call Refresh().

WARNING

This will not pervent showing of changes in each item itself, only the list.

like image 32
Aliostad Avatar answered Oct 24 '22 20:10

Aliostad


You could make a deep copy of the object you want to edit. This way, you can act on the copy while editing, without interfering with the original that remains in the list. Once you`re done editing, you can replace the original by the edited version or rollback.

like image 35
Matthieu Avatar answered Oct 24 '22 18:10

Matthieu