Capturing the Enter key in a TextBox
Solution 1:
You need to change the UpdateSourceTrigger
on your TextBox.Text
binding to PropertyChanged
. See here.
Solution 2:
You can do this by passing the TextBox's InputBindings
property as a CommandParameter
to the Command
:
<TextBox x:Name="MyTextBox">
<TextBox.InputBindings>
<KeyBinding Key="Return"
Command="{Binding MyCommand}"
CommandParameter="{Binding ElementName=MyTextBox, Path=Text}"/>
</TextBox.InputBindings>
</TextBox>
Solution 3:
You can also do this in the code behind.
How to: Detect When the Enter Key Pressed
In the check for enter/return, just invoke your event handler code.