MindFusion.Wpf Pack Programmer's Guide
Undo and Redo

Overview

Spreadsheet for WPF provides a complete undo and redo support for every change in a workbook. To enable undo and redo, set the UndoEnabled property of the Workbook to true. When undo is enabled, changes are recorded in an internal undo history. Most changes are recorded automatically and some changes need to be recorded manually. To undo the most recent change, call the Undo method. To redo the most recently undone change, call the Redo method. To check if there are changes that can be currently undone and redone, use the CanUndo and CanRedo properties respectively. To get notified when the values of these properties change, handle the PropertyValueChanged event of the Workbook class.

All interactive modifications of a workbook and its worksheets are automatically recorded in the undo history. This includes creating, editing and deleting cell data, resizing worksheet headers, auto-filling, moving or resizing interactive objects, and so on. Adding and removing worksheets and named ranges to and from the Worksheets and NamedRanges collections of the Workbook are automatically recorded as well.

Programmatic changes, more specifically, property assignments, are usually not recorded automatically. To get a better understanding of how to record programmatic changes, check the following section.

Commands

The undoable changes in Spreadsheet for WPF are represented by the Command class and its subclasses. When a change occurs, an instance of the appropriate Command class is created and executed. Executing the command effectively causes the change to happen and also registers the command in the undo history. When Undo is called, the most recently executed command is undone, which causes its associated change to be reverted.

Most commands are created and executed automatically by the system. For example, when a Worksheet is added to a Workbook, a new AddItemCommand(Worksheet) command is created and executed. However, as mentioned earlier, property changes are usually not recorded automatically. To track and undo property changes in a worksheet, use the TrackChangesCommand. This command cannot be instantiated directly. To create a TrackChangesCommand object, call the StartChangeOperation method of the Workbook class. After the command has been started, the subsequent modifications to the worksheet will be recorded into this command. To complete and execute the command, either call the CommitChangeOperation method of the Workbook class or the Dispose method of the command itself. To cancel the command (and revert all recorded modifications), call the CancelChangeOperation method of the Workbook class or the Cancel method of the command.

The following example illustrates how to record several worksheet changes in a TrackChangesCommand. Note, that due to the disposable pattern, the command is automatically executed at the end of the using block.

C#  Copy Code

using (workbook.StartChangeOperation(worksheet))
{
    worksheet.Name = "My worksheet";
    worksheet.Cells["A1"].Data = 100;
}

Visual Basic  Copy Code

Using workbook.StartChangeOperation(worksheet)
    worksheet.Name = "My worksheet"
    worksheet.Cells("A1").Data = 100
End Using

Note, that the TrackChangesCommand records the changes of a single worksheet. To change several worksheets, create and execute individual TrackChangesCommand instances for each of them. This will cause several undo records to be registered in the undo history, one for each worksheet. The CompositeCommand can be used to avoid this.

Composite Commands

The CompositeCommand is a special command that can be used to wrap up several commands into a single operation. Similarly to the TrackChangesCommand, the CompositeCommand class cannot be instantiated directly. To create a CompositeCommand object, call the StartCompositeOperation method of the Workbook class. After a composite command has been started, executing other commands will register them as children of this CompositeCommand, rather than directly in the undo history. To complete the composite action, either call the CommitCompositeOperation method of the Workbook class or the Dispose method of the command itself. To cancel an active composite command (and revert all of its subactions), call the CancelCompositeOperation method of the Workbook class or the Cancel method of the command.

The following example creates a new worksheet in an existing workbook and changes the name of this new worksheet, both as a single composite operation. Note, that due to the disposable pattern, the composite command is automatically executed at the end of the using block.

C#  Copy Code

using (workbook.StartCompositeOperation())
{
    var worksheet = workbook.Worksheets.Add();
    using (workbook.StartChangeOperation(worksheet))
        worksheet.Name = "My worksheet";
}

Visual Basic  Copy Code

Using workbook.StartCompositeOperation()
    Dim worksheet = workbook.Worksheets.Add()
    Using workbook.StartChangeOperation(worksheet)
        worksheet.Name = "My worksheet"
    End Using
End Using