This is the online documentation for PostSharp 5.0.
Download PDF or CHM. Go to v4.3 or v5.0

Adding Undo/Redo to the User Interface

The Undo/Redo functionality that you added to your codebase needs to be made available to the users. Users will want to have the ability to move forwards and backwards through the operations that they too and have been recorded.

This topic contains the following sections:

Using the ready-made WPF controls

PostSharp includes two button controls UndoButton and RedoButton that you can add to your application.

To add Undo/Redo to a WPF window:

  1. Install the PostSharp.Patterns.Xaml package using NuGet.

  2. Add the following namespace declaration to the root element of your XAML file:

    C#
    xmlns:model="clr-namespace:PostSharp.Patterns.Xaml;assembly=PostSharp.Patterns.Xaml"
  3. To add Undo and Redo buttons to the user interface, include the following two lines of Xaml.

    C#
    <model:UndoButton HorizontalAlignment="Left" Margin="22,24,0,0" VerticalAlignment="Top" />
    <model:RedoButton HorizontalAlignment="Left" Margin="64,24,0,0" VerticalAlignment="Top"/>
    simpleundoredo 2

    Your users are now able to make a changes in the user interface and Undo and/or Redo those changes at any point that they want.

Clearing the initial history

If we were to open the Customer management screen you would notice that the Undo button has a number of actions listed under it.

simpleundoredo 3

Those actions are listing the changes that were taken when the different Person instances were loaded and their properties were set. Most users will only want to see actions that they have manually taken in the screen. As such, you will need to manually interact with the Recorder to ensure that the Undo button list is empty when the window opens.

To provide an empty list of recorded actions when the windows is initially opened, open the ViewModelMain class and find the constructor. Add the following as the last line in the constructor:

C#
RecordingServices.DefaultRecorder.Clear();

The Recorder class is accessed through the RecordingServices.DefaultRecorder property. This property contains the current Recorder instance that is being used by the RecordableAttribute aspect. The Recorder class has two collections, UndoOperations and RedoOperations, which contain all of the past operations that can be undone and redone. The Clear() method removes all operations from both of those collections.

Now when you open the Customer management screen both the Undo and Redo buttons will show no history. This is the simplest type of Undo/Redo implementation that you can do. It will record each property change operation separately in the Undo and Redo UI buttons which probably isn’t what you, or your users, will want to see. Read Customizing Undo/Redo Operation Names to learn how to record groupings of operations that make sense to your business users.

Creating custom undo/redo controls

If the buttons provided by PostSharp don't meet your requirements, you can create your own controls for WPF, Windows Phone or WinRT.

Custom controls will typically provide a front-end to the global Recorder exposed on the RecordingServices.DefaultRecorder property, and we provide a view for the UndoOperations and RedoOperations collections. Controls typically use the Recorder.Undo() and Recorder.Redo() methods.