取消 Silverlight 中的双向数据绑定

发布于 2024-07-27 16:36:20 字数 279 浏览 2 评论 0原文

我有一个数据项的 ObervableCollection。 该集合绑定到一个ListBox。 当用户从列表框中选择一个项目并单击“编辑”按钮时,将显示包含该项目详细信息的 UserControl,以及绑定到文本框的各种属性。 每种绑定模式均设置为 TwoWay。 在此详细信息 UserControl 上,我想实现 2 个按钮,“确定”和“取消”。 这与 Windows 中的 UI 一致。 该应用程序使用模型-视图_视图模型模式。 这是我的问题:

1)当所有更改都已提交时,如何实现“取消”按钮?

I have an ObervableCollection of data items. This collection is bound to a ListBox. When the user selects a item from the listbox and clicks the Edit button a UserControl with the details of that item is displayed with the various properties bound to text boxes. Each binding mode is set for TwoWay. On this details UserControl, I would like to implement 2 buttons, OK and Cancel. This would be consistent with UIs in Windows. This application is using the Model-View_ViewModel pattern. Here is my question:

1) How can I implement the Cancel button when all the changes have already been committed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

娇俏 2024-08-03 16:36:20

您可以绑定到真实集合的副本,然后仅在单击“确定”时提交更改,或者您可以重新处理数据对象以允许事务处理更改,从而您可以根据对话框结果调用提交或回滚。

You could bind to a copy of the real collection and then only commit the changes on an OK click, or you could rework your data objects to allow transacted changes whereby you can call Commit or Rollback depending on the dialog result.

忱杏 2024-08-03 16:36:20

简短的回答是你不能。 SL 不会将数据对象中的原始值存储在任何位置,因此除非您更改流程,否则无法恢复因数据绑定而进行的更改。

我建议您先复制要编辑的对象,然后再将其放入对话框中。 如果您的对象很简单,您可以轻松添加“MakeCopy”成员:

public MyObject MakeCopy()
{
    return (MyObject)base.MemberwiseClone();
}

The short answer is that you can't. The original values in the data object aren't stored anywhere by SL, so unless you change your process you can't revert the changes that were made as a result of databinding.

I'd suggest making a copy of the object you want to edit before putting it in the dialog box. If your object is simple, you can easily add a "MakeCopy" member:

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