使用绑定取消WPF中的更新 [英] Canceling an update in WPF with binding

查看:169
本文介绍了使用绑定取消WPF中的更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这更像是一个概念性的问题。如果有帮助,可以说它属于MVVM模式。我从未在WPF Binding的任何一项研究中看到这一点。



如果您有一个窗口,通过绑定和用户将元素连接到视图模型更新记录,但在保存之前决定取消更新,您认为逆转更改的最佳方法是什么?



我可以想到几种可能性:


  1. 不要让用户更新主显示,而是弹出一个第二个窗口进行编辑,这个窗口被绑定到VM和保存,将数据写入原始VM,然后更新回模型。

  2. 创建原始VM对象的原始副本,并在取消时将该VM写回在原来的时间(可以删除该副本)。

  3. 创建虚拟机内每个元素的副本,这些元素在创建时可能会更改,而在取消将这些值写回更改的值。增加虚拟机的大小和复杂性。

  4. 执行取消时,虚拟机命中数据源并重新加载记录。 导致网络流量和数据库访问流量,并可能导致显示更新回到原始状态的滞后时间。如果虚拟机从另一个进程更新,并且这些外部更新不应该被删除,也可能会导致问题。

任何人都可以想到一个更好的解决方案,或者知道WPF中处理这种自动的一些模糊功能?

解决方案

我已经在我的ViewModel上实现了IEditableObject,并且通过调用EditCommand进入编辑模式时,我复制了一些字段。



我还有可以调用的SaveCommands当确认编辑。或者使用CancelEdit命令恢复到复制的值。


This is more of a conceptual question. If it helps, lets say it falls into the MVVM pattern. I've never seen this addressed in any of my research into WPF Binding.

If you have a window where you have elements connected to view-model through binding and the user updates the record, but before saving decides to cancel the update, what would you consider the best method of reversing the changes would be?

I can think of a couple of possibilities:

  1. Instead of allowing the user to update the main display, have a second window pop up for editing that is bound to a copy of the VM and upon "Save", writes that data into the original VM and then updates back to the model. Increases memory usage and complexity of the Save function.
  2. Make a "pristine" copy of the original VM object and, upon "Cancel", write that VM back over the original (at which time the copy can be deleted). Increases memory usage and complexity of the Cancel function.
  3. Create a copy of each of the elements within the VM that might change upon creation and upon "Cancel", write those values back over the changed ones. Increases the size and complexity of the VM.
  4. Upon executing "Cancel", the VM hits the data source and reloads the record. Causes network traffic and DB access traffic and possibly lag time for display updating back to original state. Also might cause problems if the VM was updated from another process and these "external" updates should not be deleted.

Can anyone think of a better solution to this problem, or know of some obscure functionality within WPF that handles this "automatically"?

解决方案

I have implemented the IEditableObject on my ViewModels and I make a copy of the fields when entering Edit mode by invoking an EditCommand.

I also have SaveCommands that can be called in when confirming the edit. Or use a CancelEdit command to revert to the copied values.

这篇关于使用绑定取消WPF中的更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆