Running the viewmodels command to enter into a TextBox

I want to execute a command in my view model when a user clicks on an input in a text box. The command works when attached to a button.

<Button Content="Add" Command="{Binding Path=AddCommand}" /> 

But I can't get it to work from a TextBox. I tried Inputbinding, but that didn't work.

 <TextBox.InputBindings> <KeyBinding Command="{Binding Path=AddCommand}" Key="Enter"/> </TextBox.InputBindings> 

I also tried to set the working button by default, but it will not be executed when I press enter.

Thanks for your help.

+48
command wpf textbox
Aug 05 '10 at 10:45
source share
6 answers

I have been trying for years to remove this, but the accepted answer cannot be deleted. See https://stackoverflow.com/a/414829/

0
Aug 06 '10 at
source share
— -

I know I'm late for the party, but I did it for me. Try using Key="Return" instead of Key="Enter"

Here is a complete example

 <TextBox Text="{Binding FieldThatIAmBindingToo, UpdateSourceTrigger=PropertyChanged}"> <TextBox.InputBindings> <KeyBinding Command="{Binding AddCommand}" Key="Return" /> </TextBox.InputBindings> </TextBox> 

Be sure to use UpdateSourceTrigger=PropertyChanged in your binding, otherwise the property will not be updated until the focus is lost and pressing the enter button loses focus ...

Hope this was helpful!

+121
Jan 14 '14 at 9:43
source share

You probably did not make the command a property, but a field. It works only for binding to properties. Change your AddCommand to a property and it will work. (Your XAML works fine for me with a property instead of a field for command -> no code needed!)

+13
Jul 11 '11 at 16:51
source share

This is where the dependency property that I created for this is created. The advantage of this is that your text binding is updated to the ViewModel before the command is run (useful for silverlight, which does not support the property generated by the modified update source).

 public static class EnterKeyHelpers { public static ICommand GetEnterKeyCommand(DependencyObject target) { return (ICommand)target.GetValue(EnterKeyCommandProperty); } public static void SetEnterKeyCommand(DependencyObject target, ICommand value) { target.SetValue(EnterKeyCommandProperty, value); } public static readonly DependencyProperty EnterKeyCommandProperty = DependencyProperty.RegisterAttached( "EnterKeyCommand", typeof(ICommand), typeof(EnterKeyHelpers), new PropertyMetadata(null, OnEnterKeyCommandChanged)); static void OnEnterKeyCommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) { ICommand command = (ICommand)e.NewValue; FrameworkElement fe = (FrameworkElement)target; Control control = (Control)target; control.KeyDown += (s, args) => { if (args.Key == Key.Enter) { // make sure the textbox binding updates its source first BindingExpression b = control.GetBindingExpression(TextBox.TextProperty); if (b != null) { b.UpdateSource(); } command.Execute(null); } }; } } 

You use it as follows:

 <TextBox Text="{Binding Answer, Mode=TwoWay}" my:EnterKeyHelpers.EnterKeyCommand="{Binding SubmitAnswerCommand}"/> 
+5
Nov 10 2018-10-11
source share

You need to define a Gesture instead of the Key KeyBinding property:

 <TextBox.InputBindings> <KeyBinding Gesture="Enter" Command="{Binding AddCommand}"/> </TextBox.InputBindings> 
+2
Feb 19 '15 at 5:24
source share

In response to Mark Heath, I took one step at a time, thus doing the property of the attached Command Parameter;

 public static class EnterKeyHelpers { public static ICommand GetEnterKeyCommand(DependencyObject target) { return (ICommand)target.GetValue(EnterKeyCommandProperty); } public static void SetEnterKeyCommand(DependencyObject target, ICommand value) { target.SetValue(EnterKeyCommandProperty, value); } public static readonly DependencyProperty EnterKeyCommandProperty = DependencyProperty.RegisterAttached( "EnterKeyCommand", typeof(ICommand), typeof(EnterKeyHelpers), new PropertyMetadata(null, OnEnterKeyCommandChanged)); public static object GetEnterKeyCommandParam(DependencyObject target) { return (object)target.GetValue(EnterKeyCommandParamProperty); } public static void SetEnterKeyCommandParam(DependencyObject target, object value) { target.SetValue(EnterKeyCommandParamProperty, value); } public static readonly DependencyProperty EnterKeyCommandParamProperty = DependencyProperty.RegisterAttached( "EnterKeyCommandParam", typeof(object), typeof(EnterKeyHelpers), new PropertyMetadata(null)); static void OnEnterKeyCommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) { ICommand command = (ICommand)e.NewValue; Control control = (Control)target; control.KeyDown += (s, args) => { if (args.Key == Key.Enter) { // make sure the textbox binding updates its source first BindingExpression b = control.GetBindingExpression(TextBox.TextProperty); if (b != null) { b.UpdateSource(); } object commandParameter = GetEnterKeyCommandParam(target); command.Execute(commandParameter); } }; } } 

Using:

 <TextBox Text="{Binding Answer, Mode=TwoWay}" my:EnterKeyHelpers.EnterKeyCommand="{Binding SubmitAnswerCommand}" my:EnterKeyHelpers.EnterKeyCommandParam="your parameter"/> 
0
Sep 21 '15 at 21:32
source share



All Articles