views:

69

answers:

4

Hello,

I am using observables collection 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

+3  A: 

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.

Aliostad
It will prevent the view from refreshing when items are added to or removed from the collection, but it won't prevent the items from raising their own `PropertyChanged` notifications, which will be reflected in the view...
Thomas Levesque
Yes, you are right. I thought only the list is the question here. I will update my answer.
Aliostad
Thank you. My main purpose is the block edit changes, not add/remove changes.
OrPaz
+2  A: 

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.

Matthieu
Thank you. I have tried this approach before, but as it uses serialization of the nested objects, some objects could not be serialized and the cloned object could not be perfectly used further on.
OrPaz
@OrPaz : being able to backup your objects one way or another, may prove critical, especially if you consider using the IEditableObject interface as suggested by Thomas.
Matthieu
I have tried deep copying the object by using serialization or entity cloners which are available. Although it works, i cant use them as it actually takes almost 5(!) seconds to perform the copy....Any suggestions? (followed up a new question here : http://stackoverflow.com/questions/4034477/entity-framework-attach-exception-after-clone )
OrPaz
Thank you. Your answer has lead me to the solution. I am posting the full details below.
OrPaz
+4  A: 

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

Thomas Levesque
Thank you. Although i am interested in that approach, the project download link seems to be broken.
OrPaz
A: 

All the anwers above are great. but i found a good and convinent prodedure to perform the desired in an efficient and clean way. It is based on performing a deep copy on a detached object, using Matthieu MEZIL entity cloner ( http://msmvps.com/blogs/matthieu/archive/2008/05/31/entity-cloner.aspx ).

For full details please check out the followings : http://stackoverflow.com/questions/4034477/entity-framework-attach-exception-after-clone

Thanks for all the great support...

OrPaz