【问题标题】:AvalonEdit :: Ctrl + I KeyBinding Doesn't WorkAvalonEdit :: Ctrl + I KeyBinding 不起作用
【发布时间】:2015-04-13 16:37:58
【问题描述】:

我正在使用 AvalonEdit(惊喜)创建一个文本编辑器。我已将 KeyBindings 添加到声明中:

<ae:TextEditor x:Name="TextEditor" ... >
   <ae:TextEditor.InputBindings>
      <KeyBinding Command="ToggleBold" Key="B" Modifiers="Control"/>
      <KeyBinding Command="ToggleItalic" Key="I" Modifiers ="Control"/>
      <!-- other bindings -->
   </ae:TextEditor.InputBindings>
</ae:TextEditor>

我有大约 20 个与典型命令相关的按钮,它们都在工作,包括 EditingCommands.ToggleItalic。我有与命令关联的 KeyBindings,它们都按预期工作除了 Ctrl+I。我无法让 Ctrl+I 键绑定组合与 any 命令一起使用(例如,尝试将它与 ToggleBold 一起使用)。

要明确:

  1. ToggleItalicKeyBinding 如果我绑定到不是的东西 Ctrl+I - Ctrl+Shift+I,例如,工作完美。
  2. Ctrl+I 组合似乎不适用于任何 KeyBinding。

有人知道为什么会这样吗?我不想偏离默认的 KeyBindings - Ctrl+I for ToggleItalics 对于我们这些喜欢我们的键盘快捷键的人来说是根深蒂固的。

【问题讨论】:

    标签: wpf wpf-controls key-bindings avalonedit


    【解决方案1】:

    AvalonEdit 控件中的Ctrl+I KeyGesture 已经与IndentSelection AvalonEditCommand 关联(它是一个RoutedCommand,因此它可以有一个或多个InputGestures)。

    如果你看一下AvalonEditCommands 类,你会发现这个代码:

    public static readonly RoutedCommand IndentSelection = new RoutedCommand( 
        "IndentSelection", typeof(TextEditor), 
        new InputGestureCollection { 
            new KeyGesture(Key.I, ModifierKeys.Control) 
    });
    

    因此,您必须删除 IndentSelection CommandBinding(在 EditingCommandHandler 类中)才能将 Ctrl+I KeyGesture 用于另一个命令。

    编辑 我在想你可以尝试通过在Application.OnStartup 方法中清除 IndentSelection 命令的InputGestureCollection 来解决你的问题:

    protected virtual void OnStartup(StartupEventArgs e)
    {
        AvalonEditCommands.IndentSelection.InputGestures.Clear();
        /* If you want now you can add a new inputgesture */
        /* The rest of your code... */
    }
    

    我没有测试这个解决方案,但我想它可以工作。

    【讨论】:

    • 如何访问该课程?我在任何 TextEditor 属性上都找不到它的实例...它们在哪里隐藏它?
    • @ScottSEA,我用另一种(更简单)的解决方案编辑了我的答案。
    猜你喜欢
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    相关资源
    最近更新 更多