Reputation: 576
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>
Upvotes: 1
Views: 1520
Reputation: 169270
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!");
}
}
}
Upvotes: 2