如何在编辑之前保存 DataGridView 单元格的值以用于撤消目的?
我正在使用命令设计模式为 DataGridView 添加撤消功能。
要创建 EditCommand,我必须知道单元格的先前值是什么,以便在用户点击撤消时可以重新输入它。
我正在使用 CellBeginEdit 和 CellEndEdit 事件来完成此操作。
使用 CellBeginEdit,我将单元格的值保存在私有变量中,并在 CellEndEdit 函数中,使用保存的值创建 EditCommand。
它工作正常,但我想知道是否有更明智的方法来做到这一点。也许是一个将前一个单元格值和新单元格值结合在一起的事件。
如果我实际上可以阻止 DataGridView 自行更新并让 EditCommand 执行此操作,那就加分了。
I am adding an undo feature for a DataGridView using the Command Design Pattern.
To create the EditCommand, I have to know what was the previous value of the cell, so I can re-enter it in case the user hits undo.
I'm doing it using the CellBeginEdit and CellEndEdit events.
With the CellBeginEdit, I save the value of the cell in a private variable and at the CellEndEdit function, I use the saved value to create the EditCommand.
It's working fine, but I wonder if there is a more saner way to do it. Maybe a event that holds together the previous and the new cell value.
Bonus points if I could actually prevent the DataGridView from updating itself and let the EditCommand do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最近在一个大型 WPF 应用程序中实现了撤消/重做,并且能够将撤消/重做逻辑提取到独立库中。这可以在 CodePlex 上找到。
该库有助于保存一堆历史记录,如果用户选择撤消/重做,则可以重新应用这些历史记录。这可以替换您的私有变量,并且还允许撤消历史记录中存在多个项目。
您可以在 http://muf.codeplex.com/ 找到文档和下载
I recently implemented Undo / Redo in a large WPF application, and was able to extract the Undo / Redo logic into a stand-alone library. This is available on CodePlex.
The library helps keep a stack of history that can be re-applied if the user elects to undo / redo. This could replace your private variables and also allow for multiple items in the undo history.
You can find documentation and downloads at http://muf.codeplex.com/