0

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?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Justin
  • 2,399
  • 4
  • 31
  • 50

3 Answers3

2

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.

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • 2
    Ah. I was hoping for a more elegant way of doing it, but I don't have any more time to spare on it. Why in the world did MS think it was a good idea to add these commands without there being an option to disable them? – Justin Jan 08 '11 at 04:32
1

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 }
        );
    }
  }
 
Community
  • 1
  • 1
jez
  • 11
  • 1
0

What about trying with the gesture instead...

        <RichTextBox.InputBindings>
            <KeyBinding Command="BlockTheCommand" Gesture="CTRL+1" />
        </RichTextBox.InputBindings>
Aaron McIver
  • 24,527
  • 5
  • 59
  • 88
  • 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. – Justin Jan 07 '11 at 22:52
  • @Justin The code above is no different then your first approach; since a given command is tied to a gesture. I'm curious if you did the above; did nothing in the handler would the framework command execute or would it make its way up stream? – Aaron McIver Jan 07 '11 at 22:56
  • If I bind the gesture to a new command like that, the new command fires and swallows the CTRL-1 gesture. The ApplySingleSpace does not fire. If I make it so that the BlockTheCommand can't execute, it still swallows the gesture. – Justin Jan 07 '11 at 23:54