I have RichTextBox in WPF which contains InlineUIContainer with some custom objects. How to allow Undo event for that UIContainer?
I have RichTextBox in WPF which contains InlineUIContainer with some custom objects. How to
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First of all bind a command to your undo button.
Write a CommandBinding similar to this:
<CommandBinding Command="Undo" Executed="ExecuteUndo" CanExecute="CanExecuteUndo"/>Then set the
Contentof the RichTextBox to something like this{Binding myUndoManager.CurrentContent, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}myUndoManageris a DependencyProperty with type ofUndoManagerclass. You need to implement this class and inherit it fromDependencyObjectandINotifyPropertyChanged. within this class,CurrentContentis aDependencyPropertywhich keeps the proper content to show andPropertyChangedevent handles every changes to theContentof the RichTextBox. (you can add a collection to the class and with each call to this event, add a new item to the collection. the type of the items for this collection might have some properties like TextDifferences, ActionType, …)Then all that is left is to implement the body of
CanExecuteUndoandExecuteUndoinside your code. (e.CanExecutecould be set to true inside theCanExecuteUndo, if and only if the collection is not empty. AndExecuteUndopops out the last item from the collection and according to itsActionTypedoes the necessary actions)And don’t forget to set the
DataContextof the window(or RichTextBox) to{Binding RelativeSource={RelativeSource Self}}ifmyUndoManagerobject is in the same class as the window.