如何禁用 Ctrl+1 的默认 RichTextBox 命令?

发布于 2024-10-11 03:07:21 字数 673 浏览 3 评论 0原文

Snoop 显示该命令是“ApplySingleSpace”,但是当我尝试通过 这篇文章。像这样:

<RichTextBox.CommandBindings>
     <CommandBinding 
       Command="ApplySingleSpace" 
       CanExecute="BlockTheCommand"/>
   </RichTextBox.CommandBindings>

  private void BlockTheCommand(object sender,
     CanExecuteRoutedEventArgs e)
   {
     e.CanExecute = false;
     e.Handled = true;
   }

我的应用程序崩溃是因为没有 ApplySingleSpace 命令。 ApplySingleSpace 也不在 EditingCommands 中。

我缺少什么?

Snoop shows that the command is "ApplySingleSpace", but when I try disabling it via the method described in this article . Like this:

<RichTextBox.CommandBindings>
     <CommandBinding 
       Command="ApplySingleSpace" 
       CanExecute="BlockTheCommand"/>
   </RichTextBox.CommandBindings>

.

  private void BlockTheCommand(object sender,
     CanExecuteRoutedEventArgs e)
   {
     e.CanExecute = false;
     e.Handled = true;
   }

My app crashes because there is no ApplySingleSpace command. ApplySingleSpace is not in the EditingCommands either.

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荒路情人 2024-10-18 03:07:21

不幸的是,这不适用于
我。我尝试禁用的原因
命令是我有一个
更高层嵌套视图中的键绑定
这不会触发,因为 CTRL+1
手势正在被吞没
有键盘焦点的 Richtextbox。

使用自定义命令覆盖 KeyBinding 来执行您想要的操作而不是尝试以某种方式禁用它怎么样?

<RichTextBox.InputBindings>
    <KeyBinding Command="local:YourCommands.Cmd1" Gesture="CTRL+1" />
<RichTextBox.InputBindings>

取自此问题

Unfortunately that will not work for
me. The reason I am trying to disable
the command is that I have a
KeyBinding in a higher nested view
that is not firing because the CTRL+1
gesture is being swallowed by the
richtextbox which has keyboardfocus.

How about overwriting that KeyBinding with a custom command that does what you want instead of trying to somehow disable it?

<RichTextBox.InputBindings>
    <KeyBinding Command="local:YourCommands.Cmd1" Gesture="CTRL+1" />
<RichTextBox.InputBindings>

Taken from this question.

人间☆小暴躁 2024-10-18 03:07:21

使用此答案中的代码
如何在 C# 中以编程方式生成按键事件?
除了您想要由 richtextbox 处理的事件之外,重新触发 PreviewKeyDown 上的所有事件似乎对我有用。 (我只需要 Ctrl-C 进行复制)。当然,如果您需要的话,您可以做到只重新触发 Ctrl-1。


private void logKeyHandler(object sender, KeyEventArgs e)
{
    if (!(Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.C))
    {
        e.Handled = true;
        var routedEvent = Keyboard.KeyDownEvent; 
        this.RaiseEvent(
            new KeyEventArgs(
            Keyboard.PrimaryDevice, 
            PresentationSource.FromDependencyObject(this),
            0,
            e.Key) { RoutedEvent = routedEvent }
        );
    }
  }
 

Using the code from this answer
How can I programmatically generate keypress events in C#?
to refire all events on PreviewKeyDown other than those you want handled by the richtextbox seems to work for me. (I only need Ctrl-C for copy). Of course you could make it so it only refires Ctrl-1 if that's what you need.


private void logKeyHandler(object sender, KeyEventArgs e)
{
    if (!(Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.C))
    {
        e.Handled = true;
        var routedEvent = Keyboard.KeyDownEvent; 
        this.RaiseEvent(
            new KeyEventArgs(
            Keyboard.PrimaryDevice, 
            PresentationSource.FromDependencyObject(this),
            0,
            e.Key) { RoutedEvent = routedEvent }
        );
    }
  }
 
在梵高的星空下 2024-10-18 03:07:21

不如尝试用手势来代替......

        <RichTextBox.InputBindings>
            <KeyBinding Command="BlockTheCommand" Gesture="CTRL+1" />
        </RichTextBox.InputBindings>

What about trying with the gesture instead...

        <RichTextBox.InputBindings>
            <KeyBinding Command="BlockTheCommand" Gesture="CTRL+1" />
        </RichTextBox.InputBindings>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文