WPF ObservableCollection Edit Mode

2019-02-09 07:44发布

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

4条回答
【Aperson】
2楼-- · 2019-02-09 08:17

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-02-09 08:27

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 : Entity Framework Attach Exception After Clone

Thanks for all the great support...

查看更多
Lonely孤独者°
4楼-- · 2019-02-09 08:29

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

查看更多
等我变得足够好
5楼-- · 2019-02-09 08:34

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.

查看更多
登录 后发表回答