I am trying to get Undo/Redo keyboard shortcuts working in my WPF application (I have my own custom functionality implemented using the Command Pattern). It seems, however, tha
The TextBox
control provides an IsUndoEnabled
property that you
can set to false
to prevent the Undo feature. (If IsUndoEnabled
is true
, the Ctrl + Z keystroke triggers it.)
Also for a control that does not provide a special property you can add a new binding for the command you want to disable. This binding
can then supply a new CanExecute
event handler that always responds false. Here’s an
example that uses this technique to remove support for the Cut feature of the text box:
CommandBinding commandBinding = new CommandBinding(
ApplicationCommands.Cut, null, SuppressCommand);
txt.CommandBindings.Add(commandBinding);
and here’s the event handler that sets the CanExecute
state:
private void SuppressCommand(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = false;
e.Handled = true;
}