ApplicationCommands.Delete.Execute()

本文关键字:Execute Delete ApplicationCommands | 更新日期: 2023-09-27 17:53:49

我想执行这样一个删除命令:

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

但:

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

为false,因此不执行。

我怎样才能把它变成真的呢?

ApplicationCommands.Delete.Execute()

我认为你需要设置CommandBinding s,指定当Execute和CanExecute函数被调用时会发生什么。

下面是一个例子:

<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>

这个例子来自CommandBinding文档(稍作修改)