Search code examples
c#mvvmuwpcommand-pattern

UWP trigger command on keydown?


I am trying to execute a command when the user presses a key:

   <TextBox Command="{Binding myCommand}" Height="200" Width="200" FontSize="20"></TextBox>

Now using UWP im not entirely sure how this is accomplished. In WPF we have keybindings but here? If possible without third party libraries. How would you guys solve this? I prefer not to reinvent the wheel and create my own command implementation that works for edge cases like these.


Solution

  • Install the Behaviors NuGet if you don't have it and then:

    <TextBox Height="200" Width="200" FontSize="20">  
        <Interactivity:Interaction.Behaviors>
            <Core:EventTriggerBehavior EventName="KeyDown">
                <Core:InvokeCommandAction Command="{Binding myCommand}" />
            </Core:EventTriggerBehavior>
        </Interactivity:Interaction.Behaviors> 
    </TextBox>