WPF TextBox Intercepting RoutedUICommands

前端 未结 6 1354
自闭症患者
自闭症患者 2021-02-14 03:10

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

6条回答
  •  悲&欢浪女
    2021-02-14 03:46

    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;
    }
    

提交回复
热议问题