2

我正在使用 WPF。我想为我的 WPF 应用程序创建键盘快捷键。我创建如下。“ open ”的第一个命令绑定标记正在工作,而退出的命令绑定不工作。我不知道是什么原因。

<Window.CommandBindings>
<CommandBinding Command="Open" Executed="CommandBinding_Executed"/>
<CommandBinding Command="Exit" Executed="CommandBinding_Executed_1" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Command="Open" Key="O" Modifiers="control" />
<KeyBinding Command="Exit" Key="E" Modifiers="control"/>
</Window.InputBindings>

上面的代码出现以下错误:

无法将属性“Command”中的字符串“Exit”转换为“System.Windows.Input.ICommand”类型的对象。CommandConverter 无法从 System.String 转换。标记文件“WpfApplication2;component/window1.xaml”第 80 行位置 25 中的对象“System.Windows.Input.CommandBinding”出错。

4

1 回答 1

4

你的问题是没有退出命令。你必须自己动手。

有关内置的​​ ApplicationCommands,请参见此处

创建自己的非常容易,我使用静态实用程序类来保存我经常使用的常用命令。像这样的东西:

public static class AppCommands
{
    private static RoutedUICommand exitCommand = new RoutedUICommand("Exit","Exit", typeof(AppCommands));

    public static RoutedCommand ExitCommand
    {
        get { return exitCommand; }
    }

    static AppCommands()
    {
        CommandBinding exitBinding = new CommandBinding(exitCommand);
        CommandManager.RegisterClassCommandBinding(typeof(AppCommands), exitBinding);
    }
}

然后你应该能够像这样绑定它:

<KeyBinding Command="{x:Static local:AppCommands.Exit}" Key="E" Modifiers="control"/>
于 2012-03-20T13:34:05.197 回答