Reputation: 53
I have a WPF application in C# that follows MVVM pattern. I have a written the following code in xaml for double click event in a datagrid that will invoke a command.
<DataGrid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick"
Command="{Binding LoadDetailGridCommand}">
<MouseBinding.CommandParameter>
<MultiBinding Converter="{StaticResource Converter}">
<Binding ElementName="dgInvDetails" Path="SelectedItem"/>
<Binding ElementName="dgInvDetails" Path="CurrentColumn"/>
</MultiBinding>
</MouseBinding.CommandParameter>
</MouseBinding>
</DataGrid.InputBindings>
I want the same to be triggered when enter key is pressed. Do I have to use the same code with KeyBinding
and set Enter
key for the same command or is there any better way of doing this?
Thanks in Advance!
Upvotes: 2
Views: 624
Reputation: 169210
If you want to reuse the binding, you could define it as a resource:
<Window.Resources>
<local:MultiConverter x:Key="Converter" />
<MultiBinding x:Key="binding" Converter="{StaticResource Converter}">
<Binding ElementName="dgInvDetails" Path="SelectedItem"/>
<Binding ElementName="dgInvDetails" Path="CurrentColumn"/>
</MultiBinding>
</Window.Resources>
...and then use a custom markup extension to reference it:
<DataGrid.InputBindings>
<KeyBinding Key="Return" Command="{Binding LoadDetailGridCommand}"
CommandParameter="{local:BindingResourceExtension binding}" />
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding LoadDetailGridCommand}"
CommandParameter="{local:BindingResourceExtension binding}" />
</DataGrid.InputBindings>
Upvotes: 1
Reputation: 109
You need to specify key binding for enter key.
Try below
<DataGrid>
<DataGrid.InputBindings>
<KeyBinding Command="{Binding LoadDetailGridCommand}" Key="Enter" >
<KeyBinding.CommandParameter>
<MultiBinding Converter="{StaticResource Converter}">
<Binding ElementName="dgInvDetails" Path="SelectedItem"/>
<Binding ElementName="dgInvDetails" Path="CurrentColumn"/>
</MultiBinding>
</KeyBinding.CommandParameter>
</KeyBinding>
</DataGrid.InputBindings>
</DataGrid>
Upvotes: 1