我需要實現一個紀念品撤消重做模式。我的應用程序有多個選項卡,在這些選項卡中有多個控件,它們都實現了 Orc.Memento。我遇到的問題是使用 MainWindow 上的菜單按鈕調用撤消,并在最后一個活動控件上的按鈕操作調用撤消中調用撤消。編輯:不幸的是,這個項目不遵循 MVVM。我選擇 Orc.Memento 是因為它在不修改對象的情況下非常容易實現。我現在只有使用鍵盤命令 Ctrl+X 和 Ctrl+Y 才能很好地工作。調用 undo 只會在活動控件上撤消。但是,當我單擊 MainWindow 菜單上的撤消/重做按鈕時,我的代碼不知道調用撤消/重做的最后一個活動控件。選項1選項一是通過設置GotFocus()每個控件的全局屬性來跟蹤最后一個活動控件。我覺得必須有更好的方法。選項 2這就是我在這里的原因:-)??刂苝ublic class MyControl : IMemento{ private MementoService mementoService = new MementoService(); public void RegisterAll() { mementoService.RegisterObject(myObject); mementoService.RegisterCollection(myCollection); } public void Undo() { mementoService.Undo(); } public void Redo() { mementoService.Redo(); }}主窗口Ctrl+Z & Ctrl+Y 映射在這里。撤消/重做方法找到當前活動的控件并在該控件上調用撤消/重做。public MainWindow{ /// <summary> /// Call undo on the currently active control /// </summary> public void Undo() { /* * get current focused control. * find the parent that is an IMemento. And call Redo on that control */ var focusedControl = FocusManager.GetFocusedElement(this); var mementoControl = UIHelper.TryFindParentThatIsIMemento<Control>(focusedControl as DependencyObject); /* * Call Undo on the control that is currently active */ if (mementoControl != null && mementoControl is IMemento) { var mem = (mementoControl as IMemento); mem.Undo(); } }}注意:如果我可以通過自動導航到發生撤消/重做的控件來編程 Excel 的工作方式,那就太好了。這是沒有必要的,但如果你有一個想法,我的耳朵是敞開的。
1 回答

德瑪西亞99
TA貢獻1770條經驗 獲得超3個贊
以下是一些建議:
嘗試針對模型實現撤消/重做(例如使用 Orc.ProjectManagement),而不是針對視圖(因為視圖是短暫的
嘗試使用 Orc.Controls 中的 TabControl,它允許您保持所有選項卡處于活動狀態,從而允許重做/撤消)。
- 1 回答
- 0 關注
- 216 瀏覽
添加回答
舉報
0/150
提交
取消