Undo Redo Commands in WPF Diagram (SfDiagram)

5 May 20213 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="#u7utulwx7fg71tofobnatnbt77h38f24-xml" aria-controls="home" role="tab" data-toggle="tab" data-original-lang="xaml">XAML</a></li><div role="tabpanel" class="tab-pane" id="u7utulwx7fg71tofobnatnbt77h38f24-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