WPF 绑定中的撤消
如何使用 WPF 中的绑定提供撤消/重做?
例如 您可以通过绑定实现主从视图。 编辑后,您的更改将使用绑定自动保存。 然后您想要撤消更改。
WPF 的绑定中有现成的东西吗? WPF 是否提供了一些结构或接口?
这个问题不是关于如何使用堆栈实现撤消/重做。
How to provide an undo / redo using bindings in WPF?
e.g.
You implement a master-detail view with bindings. After editing your changes were saved automatically using binding. Then you want to undo the changes.
Is there something ready-to-use in the binding for WPF? Does WPF provide some structures or interfaces for?
This question is not about how to implement undo/redo using stacks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 IEditableObject 接口。 它允许您拍摄实现它的对象的快照,然后在必要时回滚到该快照。
Take a look at the IEditableObject interface. It allows you to take a snapshot of the object that implements it, and then roll back to that snapshot if necessary.
您要绑定什么数据?
如果您将数据绑定到 DataSet,则可以使用 DataSet.RejectChanges() 方法撤消更改,前提是您尚未调用 DataSet.AcceptChanges()。
What are you databinding to?
If you are databinding to a DataSet you can undo the changes by using the DataSet.RejectChanges() method provided you have not already called DataSet.AcceptChanges().
您可能会发现受监控的撤消框架很有用。 http://muf.codeplex.com/
它不使用“自上而下”的命令模式,而是相反,监视发生的更改,并允许您将委托放在撤消堆栈上以反转更改。
在您的情况下,如果您绑定到底层模型/视图模型,那么您可以连接框架来捕获这些更改,然后根据需要撤消/重做它们。 如果模型实现 INotifyPropertyChanged 并使用 ObservableCollections,它应该自动反映在模型上执行的操作,包括撤消/重做操作。
您可以在 codeplex 网站 http://muf.codeplex.com/ 上找到更多信息和文档。
You may find the Monitored Undo Framework to be useful. http://muf.codeplex.com/
It doesn't use the "top down" command pattern, but instead monitors for changes as they happen and allows you to put a delegate on the undo stack that will reverse the change.
In your case, if you're binding to an underlying model / viewmodel, then you could hook up the framework to capture these changes and then undo / redo them as needed. If the model implementes INotifyPropertyChanged and uses ObservableCollections, it should automatically reflect actions performed on the model, including undo / redo actions.
You can find more info and documentation on the codeplex site at http://muf.codeplex.com/.