2

私はWPFを使用しています。WPFアプリケーションのキーボードショートカットを作成したいと思います。以下のように作成しました。「 open 」の最初のコマンドバインディングタグは機能しており、exitのコマンドバインディングは機能していません。理由はわかりません。

<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'のオブジェクト'System.Windows.Input.CommandBinding'でエラーが発生しました。行80の位置25。

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 に答える