This project is read-only.

The page has been moved to GitHub

UndoRedoManager

Package: MyToolkit
Platforms: All (PCL)

Provides almost transparent undo/redo functionality for an object graph which is composed of GraphObservableObject objects, ObservableCollections for collections and ObservableDictionarys.

The UndoRedoManager implements the command pattern and uses the GraphPropertyChangedEventArgs arguments from the GraphObservableObject to create commands based on the old and new property or collection value.

Only properties which call the GraphObservableObject's Set method are tracked for undo/redo. Multiple property changes in the same dispatcher action are grouped into one undo/redo command.

Usage

First we implement our object model based on GraphObservableObject objects and ObservableCollections:

public class Person : GraphObservableObject
{
    private string _firstName; 
    private string _lastName; 
    
    public string FirstName
    {
        get { return _firstName; }
        set { Set(ref _firstName, value); }
    }
    
    public string LastName
    {
        get { return _lastName; }
        set { Set(ref _lastName, value); }
    }
}

Now we create a new person and register it in a UndoRedoManager:

var person = new Person 
{
    FirstName = "Rico", 
    LastName = "Suter"
};

var undoRedoManager = new UndoRedoManager(person);

Now, if we change properties on the registered object graph, we can undo and redo the changes using the UndoRedoManager:

person.FirstName = "Hello"; 

undoRedoManager.Undo();
// FirstName has been reverted to "Rico"

undoRedoManager.Redo();
// FirstName is "Hello" again

Of course, the UndoRedoManager also tracks child elements (references) and elements in ObservableCollections or ObservableDictionarys (e.g. a property of type ObservableCollection with elements which inherit from GraphObservableObject).

Last edited Jun 3, 2015 at 10:53 PM by rsuter, version 20