代码之家  ›  专栏  ›  技术社区  ›  Marks

在文本框中输入时执行viewmodels命令

  •  51
  • Marks  · 技术社区  · 15 年前

    该命令在绑定到按钮时工作。

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

    但我不能把它从文本框带到工作中。 我试过输入绑定,但没用。

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

    谢谢你的帮助。

    5 回复  |  直到 15 年前
        1
  •  163
  •   mkamioner    12 年前

    我知道我参加聚会迟到了,但我得为自己做点事。尝试使用 Key="Return" Key="Enter"

    下面是完整的例子

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

    一定要使用 UpdateSourceTrigger=PropertyChanged 在绑定中,否则在焦点丢失之前不会更新属性,按enter键不会丢失焦点。。。

    希望这有帮助!

        2
  •  13
  •   Andreas Zita    14 年前

        3
  •  5
  •   Mark Heath    15 年前

    下面是我为此创建的附加依赖项属性。它的优点是确保在命令触发之前将文本绑定更新回ViewModel(对于不支持属性changed update source触发器的silverlight很有用)。

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

    你这样使用它:

    <TextBox 
        Text="{Binding Answer, Mode=TwoWay}" 
        my:EnterKeyHelpers.EnterKeyCommand="{Binding SubmitAnswerCommand}"/>
    
        4
  •  3
  •   Ashot Muradian    11 年前

    <TextBox.InputBindings>
        <KeyBinding Gesture="Enter" Command="{Binding AddCommand}"/>
    </TextBox.InputBindings>
    
        5
  •  0
  •   Community Mohan Dere    9 年前

    除了 Mark Heath 在回答这个问题时,我用这种方法实现了命令参数attached属性,使类更进一步;

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

    用法:

    <TextBox Text="{Binding Answer, Mode=TwoWay}" 
        my:EnterKeyHelpers.EnterKeyCommand="{Binding SubmitAnswerCommand}"
        my:EnterKeyHelpers.EnterKeyCommandParam="your parameter"/>
    
    推荐文章