WPF ObservableCollection 编辑模式

发布于 2024-09-29 02:51:54 字数 130 浏览 0 评论 0原文

我在我的应用程序中使用可观察的集合。我的问题是,当我使用弹出窗口编辑这些实体时,当用户更改窗口中的相应字段时,我的绑定列表也会发生更改。

我如何简单地冻结可观察的更改通知,并仅在保存实体时才释放它们?

谢谢, 奥兰

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

百善笑为先 2024-10-06 02:51:54

我认为问题不在于集合,而在于实体本身。 ObservableCollection 在添加或删除项目时引发事件,而不是在项目属性更改时引发事件。这部分由项目实现的 INotifyPropertyChanged 处理,因此您需要禁用此通知。

我建议您查看 IEditableObject界面,就是针对这种场景而设计的。您可以在 BeginEdit 方法中禁用通知,并在 EndEditCancelEdit 中重新启用它们。


编辑: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 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

乖乖 2024-10-06 02:51:54

您可以使用:

  BoundPropertyOfViewModel = CollectionViewSource.GetDefaultView(AgentDeploymentDetail);

并绑定到视图,而不是直接绑定到 ObservableCollection。这是同一个对象,允许您在不接触集合的情况下过滤/排序输出。

当您想要停止更改时,请使用DeferRefresh()。完成后,调用 Refresh()

警告

这不会阻止显示每个项目本身的更改,只会阻止显示列表。

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.

£冰雨忧蓝° 2024-10-06 02:51:54

您可以为要编辑的对象创建深层副本。这样,您可以在编辑时对副本进行操作,而不会干扰列表中保留的原始内容。完成编辑后,您可以用编辑后的版本替换原始版本或回滚。

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.

殤城〤 2024-10-06 02:51:54

上面所有的答案都很棒。但我找到了一个良好且方便的程序来以高效且干净的方式执行所需的操作。它基于使用 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文