Is there a way for aliases in WPF?

Is there a way to efficiently execute alias commands in WPF? My situation is this: I created an application that uses ApplicationCommands.Delete in the context of a graphical editor that has a number of customizable canvases. Some of the controls that are on these canvases use TextBoxes, but the problem here is: TextBox does not respond to ApplicationCommands.Delete, it responds to EditorCommands.Delete. Is there a way to clear the TextBox of the response to ApplicationCommands.Delete without subclassing or manually set the bindings on each instance of the TextBox?

+3
source share
1 answer

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);
}
+2
source

Source: https://habr.com/ru/post/1794265/


All Articles