sunnysesh
sunnysesh

Reputation: 123

Using EventTrigger to call a specific key

I currently have an EventTrigger which calls the KeyDown event, and then uses this trigger to call a ViewModel command using EventToCommand. What would be the best way to specify the key used in the KeyDown event (apart from passing the KeyEventArgs through the CommandParameter using PassEventArgsToCommand)?

Example:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="KeyDown">
        <command:EventToCommand Command="{Binding Path=TestCommand}"
                                CommandParameter="{Binding ElementName=tempListView,
                                                           Path=SelectedItem}" />
     </i:EventTrigger>
</i:Interaction.Triggers>

Any help on this would be greatly appreciated.

Upvotes: 0

Views: 1252

Answers (1)

D.Rosado
D.Rosado

Reputation: 5773

You can only bind one element using the command parameter, if you need the key event data use

<i:Interaction.Triggers>
    <i:EventTrigger EventName="KeyDown">
        <command:EventToCommand Command="{Binding Path=TestCommand}"  PassEventArgsToCommand="True"/>
     </i:EventTrigger>
</i:Interaction.Triggers>

public RelayCommand<KeyEventArgs> Command { get; private set; }

Or if you are using a Listbox for example and you want the item, this should do it:

 <i:Interaction.Triggers>
        <i:EventTrigger EventName="KeyDown">
              <command:EventToCommand Command="{Binding Path=TestCommand}"  CommandParameter="{Binding}"/>
         </i:EventTrigger>
    </i:Interaction.Triggers>

If you need the Key and the item, that's a bit more trickier, on a binded collection you can maybe combine the use of the described trigger with a binded SelectedItem, so when the command if fired you can check the selected item on your ViewModel.

Upvotes: 1

Related Questions