Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use EventTrigger and InvokeCommandAction programmatically?

Tags:

mvvm

wpf

I have a popup with a TextBox that the user should enter a ticket number into, 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's 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 on any keypress, but I really only want it to happen when the enter key is pressed. How would I go about doing that?

EDIT: I am assuming it would have to be done programmatically (hence the title), but if not that's okay too.

like image 546
Brandon Moore Avatar asked Jan 17 '13 05:01

Brandon Moore


1 Answers

You could use InputBindings - KeyBinding as alternative approach.

Something like this:

<TextBox x:Name="TicketNumber">
    <TextBox.InputBindings>
        <KeyBinding Key="Enter"
                    Command="{Binding OpenTicketCommand}" 
                    CommandParameter="{Binding ElementName=TicketNumber,
                                               Path=Text}"/>
    </TextBox.InputBindings>
</TextBox>
like image 108
Adrian Fâciu Avatar answered Oct 18 '22 14:10

Adrian Fâciu