WPF ObservableCollection 编辑模式
我在我的应用程序中使用可观察的集合。我的问题是,当我使用弹出窗口编辑这些实体时,当用户更改窗口中的相应字段时,我的绑定列表也会发生更改。
我如何简单地冻结可观察的更改通知,并仅在保存实体时才释放它们?
谢谢, 奥兰
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为问题不在于集合,而在于实体本身。
ObservableCollection
在添加或删除项目时引发事件,而不是在项目属性更改时引发事件。这部分由项目实现的INotifyPropertyChanged
处理,因此您需要禁用此通知。我建议您查看
IEditableObject
界面,就是针对这种场景而设计的。您可以在BeginEdit
方法中禁用通知,并在EndEdit
和CancelEdit
中重新启用它们。编辑:Paul Stovell 在这里有一个很好的
IEditableObject
包装器实现:http://www.paulstovell.com/editable-object-adapter
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 theINotifyPropertyChanged
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 theBeginEdit
method, and reenable them inEndEdit
andCancelEdit
.EDIT: Paul Stovell has a nice implementation of an
IEditableObject
wrapper here :http://www.paulstovell.com/editable-object-adapter
您可以使用:
并绑定到视图,而不是直接绑定到 ObservableCollection。这是同一个对象,允许您在不接触集合的情况下过滤/排序输出。
当您想要停止更改时,请使用
DeferRefresh()
。完成后,调用Refresh()
。警告
这不会阻止显示每个项目本身的更改,只会阻止显示列表。
You can use:
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, callRefresh()
.WARNING
This will not pervent showing of changes in each item itself, only the list.
您可以为要编辑的对象创建深层副本。这样,您可以在编辑时对副本进行操作,而不会干扰列表中保留的原始内容。完成编辑后,您可以用编辑后的版本替换原始版本或回滚。
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 MEZIL 实体克隆器 ( http://msmvps.com/blogs/matthieu/archive/2008/05/31/entity-cloner.aspx)。
有关完整详细信息,请查看以下内容:克隆后实体框架附加异常
谢谢感谢大家的大力支持...
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...