1

The Situation:

I have some Editing commands in WPF window, and a close command (Application.CloseCommand) and have some bindings like this

View:

 <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Close"
                        Executed="CloseCommandBinding_Executed"/>
        <CommandBinding Command="EditingCommands.ToggleBold"
                        Executed="EditingCommand_Executed"></CommandBinding>
 </Window.CommandBindings>
    <Window.InputBindings>
        <KeyBinding Key="Esc" Command="ApplicationCommands.Close"></KeyBinding>
    </Window.InputBindings>
.. *Some panel and grid stuff and more things* ..
<RichTextBox Name="RTBPopup">
       <RichTextBox.InputBindings>
            <KeyBinding Key="Esc" Command="ApplicationCommands.Close"></KeyBinding>
       </RichTextBox.InputBindings>
</RichTextBox>
.. *Some panel and grid stuff and more things* ..
<ToggleButton x:Name="btnToggleBold" CommandManager.Executed="EditingCommand_Executed" Command="EditingCommands.ToggleBold" CommandTarget="{Binding ElementName=RTBPopup}">B</ToggleButton>

Now:

If I press escape in RTBPopup (Richtextbox) the command gets executed and the debugger hits the breakpoint set on CloseCommandBinding_Executed method

but

when I click on toggle button for bold or press control + B, the EditingCommand_Executed is not getting hit by debugger (not getting executed)

What else I have tried:

 <ToggleButton.CommandBindings>
      <CommandBinding Command="EditingCommands.ToggleBold" Executed="EditingCommand_Executed"></CommandBinding>
 </ToggleButton.CommandBindings>

1 Answer 1

2

Handle the PreviewExecuted event:

<CommandBinding Command="EditingCommands.ToggleBold" 
                PreviewExecuted="CommandBinding_PreviewExecuted" />

The command is handled by the RichTextBox so it never bubbles up to your parent Window.

You could also try to use the CommandManager to hook up the event handler programmatically:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        CommandManager.AddPreviewExecutedHandler(RTBPopup, new ExecutedRoutedEventHandler(OnExecuted));
    }

    private void OnExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        if(e.Command == EditingCommands.ToggleBold)
        {
            MessageBox.Show("fired!");
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

No luck ... The command is being executed but not breakpoint hit
See my edit. You could use the CommandManager.AddPreviewExecutedHandler method.
I tried that earlier for CommandExecuted, tested for PreviewExecuted .... Still no luck .... Another part of the problem is, I can still manage the Command on button, But I really want to handle the shortcut command (Ctrl + B), that user will use for sure
Are you saying that my sample code doesn't work? It does for me so please provide a full compilable sample of your issue.
I can attest that the sample code works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.