sidanmor sidanmor - 1 month ago 6
C# Question

ApplicationCommands.Delete.Execute()

I want to execute a delete command like this:

ApplicationCommands.Delete.Execute(Keyboard.FocusedElement, Keyboard.FocusedElement);


But:

ApplicationCommands.Delete.CanExecute(Keyboard.FocusedElement, Keyboard.FocusedElement)


is false, so it's not executed.

How can I change it to be true?

Tim Tim
Answer

I believe you need to set up CommandBindings that specify what happens when the Execute and CanExecute functions are called.

Here's an example:

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Delete"
                        Executed="DeleteCommandHandler"
                        CanExecute="DeleteCanExecuteHandler" />
    </Window.CommandBindings>
    <StackPanel Name="MainStackPanel">
        <Button Command="ApplicationCommands.Delete" 
                Content="Delete File" />
    </StackPanel>
</Window>

That example comes from the CommandBinding documentation (slightly modified).

Comments