39

我有一个使用 MVVM 模式的 WPF 应用程序。将按钮连接到 VM 非常简单,因为它们实现了 ICommand。我有一个类似的上下文菜单。下一步是为上下文菜单创建快捷键。我不知道如何让快捷键调用命令。这是一个例子:

<MenuItem Header="Update" Command="{Binding btnUpdate}" >
    <MenuItem.Icon>
        <Image Source="/Images/Update.png"
               Width="16"
               Height="16" />
        </MenuItem.Icon>
    </MenuItem>

现在我添加了这个:

<Window.InputBindings>
    <KeyBinding Key="U"
                Modifiers="Control" 
                Command="{Binding btnUpdate}" />
</Window.InputBindings>

尝试将快捷键连接到相同的绑定,但这不起作用。错误是:

错误 169 无法在“KeyBinding”类型的“Command”属性上设置“Binding”。只能在 DependencyObject 的 DependencyProperty 上设置“绑定”。

没有办法将此事件与命令挂钩吗?我想不通。

提前致谢!

账单

4

5 回答 5

42

以下代码可用于将快捷键直接绑定到命令:

<Window.InputBindings>
    <KeyBinding Command="{Binding Path=NameOfYourCommand}" 
                Key="O" 
                Modifiers="Control"/>
</Window.InputBindings>

在视图的 XAML 代码中的 Window.Resources 之后添加它。

于 2012-02-19T01:23:42.840 回答
26

我编写了一个自定义标记扩展来“绑定”InputBindings到命令,它几乎可以像真正的绑定一样使用:

<UserControl.InputBindings>
    <KeyBinding Modifiers="Control" 
                Key="E" 
                Command="{input:CommandBinding EditCommand}"/>
</UserControl.InputBindings>

请注意,此标记扩展使用私有反射,因此只有在您的应用程序完全信任运行时才能使用它...

另一种选择是使用CommandReference类。可以在此处的 MVVM 工具包中找到它。这可能是一种更清洁的方法,但使用起来有点复杂。

注意,在 WPF 4 中InputBinding.CommandInputBinding.CommandParameterInputBinding.CommandTarget属性是依赖属性,所以可以正常绑定

于 2010-03-04T21:56:33.863 回答
9

我同意在 XAML 中执行此操作是理想的,但为了完整起见,您还可以在代码中添加绑定。如果您在构造函数中执行此操作,只需确保它在调用之后InitializeComponent()

InputBindings.Add(new KeyBinding(btnUpdate, new KeyGesture(Key.U, ModifierKeys.Control));
于 2010-05-25T13:43:52.527 回答
0

WPF 应用程序框架 (WAF)项目的 ShortcutKey 示例中显示了将 WPF 快捷键绑定到 ViewModel 的 Command 属性的另一种方法。

于 2010-03-07T19:12:01.283 回答
0

已经能够在 DataGrid 级别上添加键绑定。像这样 :

xml:

<DataGrid 
                    AutoGenerateColumns="False"
                    ItemsSource="{Binding YourCollection}"                         
                    CanUserAddRows="False"                        
                    HeadersVisibility="Column" 
                    CanUserDeleteRows="False" 
                    CanUserSortColumns="True"
                    CanUserResizeRows="False"
                    CanUserResizeColumns="False"                       
                    SelectedItem="{Binding YourSelectedItem}" 
                    SelectionMode="Single" 
                    SelectionUnit="FullRow"
                   >
                <DataGrid.ContextMenu>
                    <ContextMenu>
                       **<MenuItem Header="Delete" InputGestureText="Del" Command="{Binding DeleteCommand}">**
                        </MenuItem>
                    </ContextMenu>
                </DataGrid.ContextMenu>
                **<DataGrid.InputBindings>
                    <KeyBinding Key="Delete" Command="{Binding DeleteCommand}" CommandParameter="Delete"/>**
                </DataGrid.InputBindings>
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Column Header" Binding="{Binding YourColumn}" IsReadOnly="True" />
                </DataGrid.Columns>
</DataGrid>

查看型号:

public ICommand DeleteCommand
            {
                get
                {
                    return new DelegateCommand(ExecuteCommand, CanExecute);
                }
            }

  private void ExecuteCommand()
{
// your code to delete here.
   YourCollection.Remove(YourSelectedItem);
}

private void CanExecute()
{
// logic to check if the delete command can execute.
   return YourSelectedItem != null ;
}
于 2016-08-08T10:14:37.390 回答