Undo Redo Commands in WPF SfDiagram

18 Nov 20181 minute to read

The Undo command reverses the last editing action performed. For example, some of the basic operations performed on diagram objects such as translation, rotation, resizing, grouping, ungrouping, changing z-order, addition, deletion, and so on, can be reversed. The Redo command restores the last editing action if no other actions have occurred since the last undo.

Undo and Redo actions are disabled by default, to enable this you can use the Constraints property of the SfDiagram. Please refer to the GraphConstraints.

NOTE

Undo and Redo commands are available only when the GraphConstraints.Undoable constraint is enabled on the SfDiagram. Ensure that this constraint is set before executing the Undo or Redo commands.

<Syncfusion:SfDiagram x:Name="diagramcontrol" Constraints="Default,Undoable"/>

<Button Height="50" Content="Undo" Name="Undo" Command="Syncfusion:DiagramCommands.Undo"></Button>

<Button Height="50" Content="Redo" Name="Redo" Command="Syncfusion:DiagramCommands.Redo"></Button>
//Initialize the SfDiagram 
SfDiagram diagramcontrol = new SfDiagram();

// To enable the Undo and Redo action
diagramcontrol.Constraints |= GraphConstraints.Undoable;

IGraphInfo graphinfo = diagramcontrol.Info as IGraphInfo;

// To perform the Undo action in Diagram
graphinfo.Commands.Undo.Execute(null);

// To Perform the Redo action in Diagram
graphinfo.Commands.Redo.Execute(null);

NOTE

The Undo command reverses the most recent tracked action in the diagram. If there are no actions available in the undo stack, the command will not perform any operation.

NOTE

The Redo command restores the most recently undone action. If there are no actions available in the redo stack, the command will not perform any operation.

Undo Redo gif

View sample in GitHub

See Also

How to enable Undo/Redo feature in WPF SfDiagram?