Click here to Skip to main content
12,556,918 members (52,119 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked

Undoing MVVM

, 1 Feb 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Providing Undo/Redo across VMs (part 1 - simple properties)
  • Download undoredosamplezip
    The usual rules apply, the download needs to be renamed from .doc to .zip.

I apologise that it’s been a while since I last blogged, but I've been busy working on an MVVM framework and it’s been eating up a lot of time – it’s good eat, but it is time consuming. One of the things I've been adding into the code is the ability to handle undo/redo functionality in a ViewModel; and more importantly, coordinating undo/redo across multiple View Models. In this blog post, I'd like to demonstrate how easy it is to add this functionality to properties that support change notification. In a future blog, I'll be demonstrating how to extend this to supporting ObservableCollections as well.

The first thing that we're going to do is define a simple undo/redo interface. Here it is, in all its glory:

using System;
namespace UndoRedoSample
    /// <span class="code-SummaryComment"><summary>

Now, we need to create a class that implements this interface.

using System;
namespace UndoRedoSample
    /// <span class="code-SummaryComment"><summary>

This class simply wraps a property. Whenever Undo is called, reflection is used to set the property back to its prechanged value. Calling Redo reverses this change. Now, that’s all well and good, but we need to keep track of these changes and apply them – and more importantly, we need to apply them across ViewModels. This is where the UndoManager comes in:

using System;
using System.Collections.Generic;
using System.Linq;

namespace UndoRedoSample
    /// <span class="code-SummaryComment"><summary>

In this code, we have two lists – the undoable list and the redoable list. These lists wrap up the IUndoRedo interface we defined earlier and will actually handle calling Undo or Redo as appropriate. There’s a little wrinkle when you call Undo – we need to copy the Redo list out to a temporary copy so that we can add it back later on. The way that the undo works, is to extract the last item from the undo stack – which then gets removed. This item is put onto the redo stack so that we can redo it later if need be. If you notice, in the Add method, we clear the Redo stack so that we can’t perform a Redo after a new operation. As the property gets updated and triggers the Add method, we have to copy the Redo out, and add it back in after the Add has been performed.

All you need to do now, is wire your ViewModel up to the UndoManager and Robert’s your mother's brother. I’ve attached a sample application which demonstrates this in action – this application isn’t finished yet as we're leaving room for the next installment, where we hook into an undoable observable collection. Here’s a screenshot of the application in action:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Pete O'Hanlon
United Kingdom United Kingdom
A developer for over 30 years, I've been lucky enough to write articles and applications for Code Project as well as the Intel Ultimate Coder - Going Perceptual challenge. I live in the North East of England with 2 wonderful daughters and a wonderful wife.

I am not the Stig, but I do wish I had Lotus Tuned Suspension.

You may also be interested in...


Comments and Discussions

GeneralHave a 5 Pin
zagitta9-Feb-10 14:07
memberzagitta9-Feb-10 14:07 
GeneralRe: Have a 5 Pin
Pete O'Hanlon9-Feb-10 21:47
mvpPete O'Hanlon9-Feb-10 21:47 
GeneralA few comments Pin
kornman004-Feb-10 3:27
memberkornman004-Feb-10 3:27 
GeneralRe: A few comments Pin
Pete O'Hanlon4-Feb-10 3:49
mvpPete O'Hanlon4-Feb-10 3:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161026.1 | Last Updated 1 Feb 2010
Article Copyright 2010 by Pete O'Hanlon
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid