DataGrid Multiple KeyBinding to same function,如何知道我按下了哪个键

时间:2012-09-03 09:14:58

标签: wpf mvvm key-bindings

我有这个xaml:

<DataGrid.InputBindings>
        <KeyBinding Key="a" Command="{Binding Path=Function}" CommandParameter="{Binding ElementName=dataGrid1,Path=SelectedItem}"></KeyBinding>
        <KeyBinding Key="s" Command="{Binding Path=Function}" CommandParameter="{Binding ElementName=dataGrid1,Path=SelectedItem}"></KeyBinding>
</DataGrid.InputBindings>

当我选择一行并按a或s时,我将所选项目绑定到KeyPressed。我的问题是我怎么知道我按下了哪个键? (我想绑定按键和选中的项目)

1 个答案:

答案 0 :(得分:2)

如果您有2个键绑定,则可以更清楚地获得2个不同的命令:

<DataGrid.InputBindings>
    <KeyBinding Key="a" Command="{Binding Path=ACommand}" CommandParameter="{Binding ElementName=dataGrid1,Path=SelectedItem}"></KeyBinding>
    <KeyBinding Key="s" Command="{Binding Path=SCommand}" CommandParameter="{Binding ElementName=dataGrid1,Path=SelectedItem}"></KeyBinding>
</DataGrid.InputBindings>