To answer your specific question, I don't know how to get two separate routed commands to be processed as the same command. But since it ApplicationCommands.Deleteis a routable command, after its delivery to its target, TextBoxand there is no command binding, it will begin to pop up. Thus, the easiest solution that suits your requirements is to set the command binding for ApplicationCommands.Deletesomewhere between TextBoxup to and possibly including Window, which implements the desired behavior.
Here is an example that sets up a parent handler Gridthat sends the “correct” command to the focused element, which in this case will be TextBox:
<Grid>
<Grid.CommandBindings>
<CommandBinding Command="ApplicationCommands.Delete" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed"/>
</Grid.CommandBindings>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_Edit">
<MenuItem Header="_Delete" Command="ApplicationCommands.Delete"/>
</MenuItem>
</Menu>
<StackPanel>
<TextBox Text="Some text"/>
</StackPanel>
</DockPanel>
</Grid>
and here the code is behind:
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
EditingCommands.Delete.Execute(null, Keyboard.FocusedElement);
}
source
share