开发者

How can I disable the default RichTextBox command for Ctrl+1?

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"/>
   </Ri开发者_Go百科chTextBox.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?


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.


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


What about trying with the gesture instead...

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜