How to programmatically use EventTrigger and InvokeCommandAction?

I have a popup with a TextBox in which the user must enter the ticket number, and then when the user presses the enter key, I want the ticket number to be passed to the ViewModel, which will retrieve the ticket.

Here is the xaml for the TextBox :

 <TextBox x:Name="TicketNumber"> <i:Interaction.Triggers> <i:EventTrigger EventName="KeyDown"> <i:InvokeCommandAction Command="{Binding OpenTicketCommand}" CommandParameter="{Binding ElementName=TicketNumber, Path=Text}"/> </i:EventTrigger> </i:Interaction.Triggers> </TextBox> 

The above works with any keystroke, but I really want this to happen when the enter key is pressed. How can I do it?

EDIT: I suppose it would have to be done programmatically (hence the name), but if that is not so good.

+8
wpf mvvm
source share
1 answer

You can use InputBindings - KeyBinding as an alternative approach.

Something like that:

 <TextBox x:Name="TicketNumber"> <TextBox.InputBindings> <KeyBinding Key="Enter" Command="{Binding OpenTicketCommand}" CommandParameter="{Binding ElementName=TicketNumber, Path=Text}"/> </TextBox.InputBindings> </TextBox> 
+19
source share

All Articles