Executing viewmodels command on enter in TextBox

后端 未结 5 1345
深忆病人
深忆病人 2020-12-24 00:23

I want to execute a command in my viewmodel when the user presses enter in a TextBox. The command works when bound to a button.

相关标签:
5条回答
  • 2020-12-24 00:51

    I know I am late to the party, but I got this to work for me. Try using Key="Return" instead of Key="Enter"

    Here is the full example

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

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

    Hope this was helpful!

    0 讨论(0)
  • 2020-12-24 00:51

    Here's an attached dependency property I created for this. It has the advantage of ensuring that your text binding is updated back to the ViewModel before the command fires (useful for silverlight which doesn't support the property changed update source trigger).

    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 like this:

    <TextBox 
        Text="{Binding Answer, Mode=TwoWay}" 
        my:EnterKeyHelpers.EnterKeyCommand="{Binding SubmitAnswerCommand}"/>
    
    0 讨论(0)
  • 2020-12-24 00:55

    In addition to Mark Heath's answer, I took the class one step further by implementing Command Parameter attached property in this way;

    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);
                    }
                };
            }
        } 
    

    Usage:

    <TextBox Text="{Binding Answer, Mode=TwoWay}" 
        my:EnterKeyHelpers.EnterKeyCommand="{Binding SubmitAnswerCommand}"
        my:EnterKeyHelpers.EnterKeyCommandParam="your parameter"/>
    
    0 讨论(0)
  • 2020-12-24 00:58

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

    <TextBox.InputBindings>
        <KeyBinding Gesture="Enter" Command="{Binding AddCommand}"/>
    </TextBox.InputBindings>
    
    0 讨论(0)
  • 2020-12-24 01:13

    You have probably not made the command a property, but a field. It only works to bind 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 the command -> no need for any code behind!)

    0 讨论(0)
提交回复
热议问题