【问题标题】:Ignore Ctrl-L in WPF RichTextBox忽略 WPF RichTextBox 中的 Ctrl-L
【发布时间】:2011-12-30 20:51:05
【问题描述】:

如何从 WPF RichTextBox 忽略/阻止/删除 Ctrl-L 键盘快捷键?

现在,这已绑定到 AlignLeft EditingCommand。我想将此键盘快捷键用于 RichTextBox 中的其他内容(删除行)。

我目前正在处理 keyDown 事件,但 Ctrl-L 从未成功。也就是说,我可以响应Ctrl-H,比如没问题,但是Ctrl-L已经被控件吞噬了。

    private void richTextBoxMain_KeyDown (object sender, KeyEventArgs e)
    {

        if ( Keyboard.IsKeyDown(Key.LeftCtrl))
        {
            if (e.Key == Key.L)
            {
                 // never gets here.
            }
        }
    }

【问题讨论】:

    标签: wpf keyboard-shortcuts richtextbox


    【解决方案1】:

    将此添加到您的页面加载方法中(或合适的地方)

    KeyBinding keyBinding = new KeyBinding(ApplicationCommands.NotACommand, Key.L, 
                                                             ModifierKeys.Control);
    richTextBoxMain.InputBindings.Add(keyBinding);
    

    这将阻止CTRL + L 调用它通常执行的命令(由于NotACommand 枚举)。您当前在 KeyDown 方法中的代码现在应该可以工作了。

    【讨论】:

    • 我有 System.Windows.Forms.RichTextBox....它在 RichTextBoxMain.InputBindings.Add(keyBinding); 行显示错误
    【解决方案2】:

    您可以简单地处理 RichtextBox 控件上的 CommandBinding,如下所示(代码隐藏或 xaml):

    richTextBoxMain.CommandBindings.Add(new CommandBinding(EditingCommands.ToggleBold, NullOnCommand, NullOnCanExecuteCommand));
    
    
    private static void NullOnCommand(object sender, ExecutedRoutedEventArgs e)
    {
        e.Handled = true;
    }
    
    private static void NullOnCanExecuteCommand(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = false;
        e.Handled = true;
    }
    

    希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      使用ShortcutsEnabled可以禁用所有RichTextBox快捷方式

      C#代码:

          RichTextBody.ShortcutsEnabled = false;
      

      vb.net 代码:

          RichTextBody.ShortcutsEnabled = False
      

      默认快捷键是:

      Ctrl-Z - 撤消

      Ctrl-E

      Ctrl-C - 复制

      Ctrl-Y

      Ctrl-退格键

      Ctrl-V - 粘贴

      Shift-删除-删除

      Ctrl-L - Align current line to the left, AlignLeft

      Shift-插入 - 粘贴

      Ctrl-R - Align current line to the right

      您可以在KeyPress事件中处理Ctrl-LCtrl-R

      C#代码:

      vb.net 代码:

      仅禁用 Ctrl-L

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-05-01
        • 1970-01-01
        • 2020-05-12
        • 2018-12-10
        • 2019-12-06
        • 1970-01-01
        • 2018-12-19
        相关资源
        最近更新 更多