Undo Redo in WPF DOCX Editor

18 Aug 20263 minutes to read

The WPF RichTextBox (SfRichTextBoxAdv) provides history preservation support, which means each editing operation performed on its document content will be preserved in history. The undo and redo behavior is configured through the EditorSettings class. You can easily undo any editing action. The undone actions will also be preserved in a separate stack enabling you to redo the action.

NOTE

Currently, the number of actions that can be preserved in both undo and redo stacks is limited to 500.

UI command to perform undo/redo operations

The following code example demonstrates how to bind the UndoCommand and RedoCommand commands for performing undo and redo operations.

<!-- Binds button to the UndoCommand -->
<Button Content="Undo" Command="RichTextBoxAdv:SfRichTextBoxAdv.UndoCommand" CommandTarget="{Binding ElementName=richTextBoxAdv}" />
<!-- Binds button to the RedoCommand -->
<Button Content="Redo" Command="RichTextBoxAdv:SfRichTextBoxAdv.RedoCommand" CommandTarget="{Binding ElementName=richTextBoxAdv}" />

NOTE

In order to perform undo/redo, the standard keyboard shortcuts such as CTRL + Z, CTRL + Y can also be used.

Enable/Disable undo redo

Disable undo for all the editing actions

In SfRichTextBoxAdv, the Undo functionality is enabled by default. You can disable it by using the IsUndoEnabled property of the EditorSettings class.

The following code example demonstrates how to disable the Undo functionality in SfRichTextBoxAdv.

<Syncfusion:SfRichTextBoxAdv x:Name="richTextBoxAdv">
	<Syncfusion:SfRichTextBoxAdv.EditorSettings>
		<Syncfusion:EditorSettings IsUndoEnabled="False"/>
	</Syncfusion:SfRichTextBoxAdv.EditorSettings>
</Syncfusion:SfRichTextBoxAdv>
// Defines the SfRichTextBoxAdv control with undo operation disabled.
SfRichTextBoxAdv richTextBoxAdv = new SfRichTextBoxAdv();
richTextBoxAdv.EditorSettings.IsUndoEnabled = false;
' Defines the SfRichTextBoxAdv control with undo operation disabled.
Dim richTextBoxAdv As New SfRichTextBoxAdv()
richTextBoxAdv.EditorSettings.IsUndoEnabled = false

NOTE

This API is supported starting from release version v18.1.0.X.

Disable undo for style modification actions

In SfRichTextBoxAdv, the Undo functionality is enabled by default for all the editing operations. If you want to disable the Undo functionality for modifying an existing style only, you can disable it by using the CanUndoStyle property of the EditorSettings class.

The following code example demonstrates how to disable the Undo support for modifying an existing style in SfRichTextBoxAdv.

<Syncfusion:SfRichTextBoxAdv x:Name="richTextBoxAdv">
	<Syncfusion:SfRichTextBoxAdv.EditorSettings>
		<Syncfusion:EditorSettings CanUndoStyle="False"/>
	</Syncfusion:SfRichTextBoxAdv.EditorSettings>
</Syncfusion:SfRichTextBoxAdv>
// Defines the SfRichTextBoxAdv control with document style undo operation disabled.
SfRichTextBoxAdv richTextBoxAdv = new SfRichTextBoxAdv();
richTextBoxAdv.EditorSettings.CanUndoStyle = false;
' Defines the SfRichTextBoxAdv control with document style undo operation disabled.
Dim richTextBoxAdv As New SfRichTextBoxAdv()
richTextBoxAdv.EditorSettings.CanUndoStyle = False

NOTE

This API is supported starting from release version v18.1.0.X.

NOTE

You can refer to our WPF RichTextBox feature tour page for its groundbreaking feature representations. You can also explore our WPF RichTextBox example to know how to render and configure the editing tool.

See also