This project is read-only.


Steps to make a VM undoable:

  1. Add a reference to the UndoRedoFramework.dll
  2. Add a using statement to the code file containing your ViewModel: 'using UndoRedoFramework.Core;'
  3. Create a private instance variable of the type UndoRedoContext: 'private UndoRedoContext _context;'
  4. Implement the INotifyPropertyChanged interface on your ViewModel explicitly, and send any values received in the add method of the declaration to the context's event of the same name. Also, don't forget to subtract any value received in the remove method from the context's PropertyChanged event. For an example of this step, download the source code, open MainWindoViewModel.cs, and scroll down to the region titled INotifyPropertyChanged members.
  5. At this point, you can begin making undoable commands, and undoable properties

To create an undoable property:

  1. Declare a regular property, but instead of backing it with a private instance field, back it with a UndoableProperty<TPropertyType>
  2. The setter should call the SetValue method of the UndoableProperty
  3. The getter should call the GetValue method of the UndoableProperty
  4. Don't forget to initialize the UndoableProperty<TPropertyType> using the context you previously created
  5. When initializing the property , don't forget to both provide the correct context, and the name of the property, enabling change notifications. For an example of these steps, download the source code, open MainWindoViewModel.cs, and scroll down to the region entitled, Properties, expand the sub-region entitled Undoable Properties, and take a look at one of the properties within.

To create an undoable command:

  1. Decide the specific requirements for the command you wish to create. For the sake of this example, I am going to create a command that deletes the last character from a string.
  2. In this case, I do not need data from the UI for executing the command (assuming the string in question is a property in my VM), but in order to undo the command, I will need to know the character that I last deleted. Thus, I choose to use UndoableDelegateCommand<TUndoRedoData> as the type of my command.
  3. I create a property named BackspaceCommand of type UndoableDelegateCommand<char>
  4. In the constructor for my class, I initialize this property to a new UndoableDelegateCommand<char>
    1. For the execute logic, I simply say: 'var undoData = str.Last; str = str.Substring(str.Length -1); return undoData;'
    2. For the can execute logic, I simply say str.Count > 0
    3. For the undo logic I say 'str += data;'
    4. For the redo logic, I copy the execute logic
    5. For the context, I pass in my VM's context
    6. I leave the name blank, to let it get a default name
  5. I can now bind to this command from the UI

To bind a context's Undo and Redo methods to a Button (or other compatible UIElement)

  1. Declare two get-only properties of type ICommand in your View Model, one called UndoCommand, and the other RedoCommand
  2. In the get accessor of your properties call the GetUndoCommand, and GetRedoCommand respectively on the VM's context.

Last edited Mar 8, 2010 at 9:01 PM by balasanjay, version 4


No comments yet.