Undo Redo Commands in WPF Diagram (SfDiagram)

21 Sep 20233 minutes 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

<li role="presentation" class=""><a data-target="#02u9jvcwf44owjrnhm2yloa0ifyz64lb-xml" aria-controls="home" role="tab" data-toggle="tab" data-original-lang="xaml">XAML</a></li><div role="tabpanel" class="tab-pane" id="02u9jvcwf44owjrnhm2yloa0ifyz64lb-xml" data-original-lang = "xaml" ><div class="highlight"><pre><code class="language-xml" data-lang="xml"><span></span><span class="nt">&lt;Syncfusion:SfDiagram</span> <span class="na">x:Name=</span><span class="s">&quot;diagramcontrol&quot;</span> <span class="na">Constraints=</span><span class="s">&quot;Default,Undoable&quot;</span><span class="nt">/&gt;</span>

<span class="nt">&lt;Button</span> <span class="na">Height=</span><span class="s">&quot;50&quot;</span> <span class="na">Content=</span><span class="s">&quot;Undo&quot;</span> <span class="na">Name=</span><span class="s">&quot;Undo&quot;</span> <span class="na">Command=</span><span class="s">&quot;Syncfusion:DiagramCommands.Undo&quot;</span><span class="nt">&gt;&lt;/Button&gt;</span>

<span class="nt">&lt;Button</span> <span class="na">Height=</span><span class="s">&quot;50&quot;</span> <span class="na">Content=</span><span class="s">&quot;Redo&quot;</span> <span class="na">Name=</span><span class="s">&quot;Redo&quot;</span> <span class="na">Command=</span><span class="s">&quot;Syncfusion:DiagramCommands.Redo&quot;</span><span class="nt">&gt;&lt;/Button&gt;</span></code></pre></div>
</div>
// 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);

Undo Redo gif

View sample in GitHub