如何设置自定义命令的默认热键?

时间:2013-09-21 14:24:29

标签: c# wpf command hotkeys

我创建了自定义命令NewEditDelet,我在InputBinding设置了这个热键,但是当我需要时它非常努力

每次都使用我的命令

Copy命令示例,我不需要设置热键,但它有默认热键Ctrl + C

如何为客户命令设置默认热键Ctrl + N,Ctrl + E和Ctrl + D?

public static class HWCommand
{
    static RoutedUICommand save = new RoutedUICommand("Save", "Save", typeof(HWCommand));
    static RoutedUICommand novo = new RoutedUICommand("Novo", "Novo", typeof(HWCommand));
    static RoutedUICommand deletar = new RoutedUICommand("Deletar", "Deletar", typeof(HWCommand));
    static RoutedUICommand editar = new RoutedUICommand("Editar", "Editar", typeof(HWCommand));

    public static RoutedUICommand Save { get { return save; } }
    public static RoutedUICommand Novo { get { return novo; } }
    public static RoutedUICommand Deletar { get { return deletar; } }
    public static RoutedUICommand Editar { get { return editar; } }
}

我的CommandBinding

<Window.CommandBindings>
    <CommandBinding Command="{x:Static hw:HWCommand.Novo}" Executed="NovoCommand_Executed"/>
    <CommandBinding Command="{x:Static hw:HWCommand.Editar}" Executed="EditarCommand_Executed" CanExecute="EditCommand_CanExecuted"/>
    <CommandBinding Command="{x:Static hw:HWCommand.Deletar}" Executed="DeletarCommand_Executed" CanExecute="DeletarCommand_CanExecuted"/>
</Window.CommandBindings>

1 个答案:

答案 0 :(得分:2)

您可以为RoutedUICommand

使用不同的构造函数
RoutedUICommand(String, String, Type, InputGestureCollection)

您可以像这样指定InputGestureCollection

static RoutedUICommand novo = new RoutedUICommand(
                                     "Novo", 
                                     "Novo", 
                                     typeof(HWCommand), 
                                     new InputGestureCollection(
                                         new InputGesture[] { new KeyGesture(Key.N, ModifierKeys.Control) }
                                     ));