2011-12-18 33 views

回答

-1

首先将命令绑定到撤消按钮。 写一个CommandBinding与此类似: <CommandBinding Command="Undo" Executed="ExecuteUndo" CanExecute="CanExecuteUndo"/>

然后设置RichTextBox中的Content到这样的事情

{Binding myUndoManager.CurrentContent, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}

myUndoManager与类型UndoManager类的一个DependencyProperty。你需要实现这个类并从DependencyObjectINotifyPropertyChanged继承它。 CurrentContentDependencyProperty,它保持显示正确的内容,而PropertyChanged事件处理RichTextBox的Content的所有更改。 (您可以将集合添加到类中,并且每次调用此事件时,都会向集合中添加一个新项目,此集合的项目类型可能具有某些属性,如TextDifferences,ActionType ...)

然后剩下的就是在代码中实现CanExecuteUndoExecuteUndo的主体。 e.CanExecute可以设置为true CanExecuteUndo内,当且仅当该集合不为空,而且ExecuteUndo从集合弹出的最后一个项目,并根据其ActionType做必要的行动)

和Don”如果myUndoManager对象与窗口在同一个类中,则忘记将窗口(或RichTextBox)的DataContext设置为{Binding RelativeSource={RelativeSource Self}}