Capture Enter key in text field

In my WPF view, I am trying to associate an event with the Enter key as follows:

<TextBox Width="240" VerticalAlignment="Center" Margin="2" Text="{Binding SearchCriteria, Mode=OneWayToSource}"> <TextBox.InputBindings> <KeyBinding Key="Enter" Command="{Binding EnterKeyCommand}"/> <KeyBinding Key="Tab" Command="{Binding TabKeyCommand}"/> </TextBox.InputBindings> </TextBox> 

This code works, and my EnterKeyCommand runs when users press the Enter key. However, the problem is that when the event fires, WPF has not yet linked the text in the text box to "SearchCriteria". Therefore, when my event fires, the contents of "SearchCriteria" is empty. Is there a simple change I can make in this code so that I can get the contents of the text box when my EnterKey command fires?

+59
wpf
Apr 05 '11 at 18:15
source share
5 answers

You need to change UpdateSourceTrigger to bind TextBox.Text to PropertyChanged . See here .

+60
Apr 05 '11 at 18:19
source share

You can also do this in code.

How to determine when the enter key is pressed

When checking input / return, just call the event handler code.

+6
Mar 15 '13 at 22:33
source share

You can do this by passing the TextBox InputBindings property as CommandParameter in Command :

 <TextBox x:Name="MyTextBox"> <TextBox.InputBindings> <KeyBinding Key="Return" Command="{Binding MyCommand}" CommandParameter="{Binding ElementName=MyTextBox, Path=Text}"/> </TextBox.InputBindings> </TextBox> 
+6
May 16 '16 at 23:07
source share

I know this is 6 years old, but none of the answers give the correct answer in full using XAML and without code. I still had to do something. For reference, the approach is as follows. Xaml first

  <TextBox Text="{Binding SearchText, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"> <TextBox.InputBindings> <KeyBinding Key="Enter" Command="{Binding SearchEnterHit}"/> <KeyBinding Key="Return" Command="{Binding SearchEnterHit}"/> </TextBox.InputBindings> </TextBox> 

Basically, the approach is that each key is thrown into the associated SearchText each time a key is pressed. Thus, the string will be fully present in SearchText after pressing the return / enter button. Thus, in the SearchEnterHit command, the entire line in the TextBox is accessible through the SearchText property.

As mentioned above, UpdateSourceTrigger = PropertyChanged is what resets each keystroke to the SearchText property. KeyBindings captures an input key.

This is indeed the easiest way to do this without code and all of XAML. I am sure that you often blush the keys to the SearchText property, but this is not a problem at all.

0
Aug 30 '17 at 13:00
source share

It can also be done as an async event. Here is an example.

 tbUsername.KeyDown += async (s, e) => await OnKeyDownHandler(s, e); private async Task OnKeyDownHandler(object sender, KeyEventArgs e) { if (e.Key == Key.Return) { if (!string.IsNullOrEmpty(tbUsername.Text) && !string.IsNullOrEmpty(tbPassword.Password)) { Overlay.Visibility = Visibility.Visible; await Login(); } } } 
0
Nov 28 '17 at 4:31 on
source share



All Articles