#935 – Setting an Undo Limit for a TextBox

By default, a TextBox control has an undo limit of 100.  That is, it keeps track of the most recent 100 edits that you’ve made, allowing you to undo them one at a time.

You can change the number of operations that the TextBox remembers by setting its UndoLimit property.  For example, if you set this value to 3, the TextBox will only allow you to undo the most recent three edits.

You may want to increase the value of UndoLimit so that the user of your application has the ability to undo a larger number of recent operations.

    <TextBox Margin="5" UndoLimit="1000"
        Text="{Binding SomeText}"
        TextWrapping="Wrap"
        VerticalScrollBarVisibility="Auto"/>

On the other hand, you may want to reduce the value of UndoLimit so that your application does need to allocate memory for all of the recent changes to a TextBox.

#934 – TextBox Has Built-In Undo Functionality

The TextBox control in WPF has built-in Undo and Redo functionality, allowing you to undo changes to the content of the TextBox or to re-apply (redo) changes that you’ve undone.

Assume that you have a TextBox with some text:

934-001

Now assume that you double-click to select the first word and then press Delete to delete it.

934-002

Do this two more times, so that you’ve delete the first three words of the text, one at a time.

934-003

You’ve now made three distinct changes to the text in the TextBox, one at a time.  You can delete the most recent change by pressing Ctrl+Z, which executes an undo operation.  This will restore the word that was most recently deleted.

934-004

You can press Ctrl+Z two more times, restoring the other words that you deleted.

934-005

You can redo a change (e.g. delete a word) by pressing Ctrl+Y.

934-006