【问题标题】:Executing viewmodels command on enter in TextBox在 TextBox 中输入时执行 viewmodels 命令
【发布时间】:2011-03-25 17:50:36
【问题描述】:

当用户在文本框中按下回车键时,我想在我的视图模型中执行一个命令。 该命令在绑定到按钮时有效。

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

但我无法从 TextBox 中使用它。 我尝试了输入绑定,但它不起作用。

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

我也尝试将工作按钮设置为默认,但按下回车键时它不会被执行。

感谢您的帮助。

【问题讨论】:

  • 您能更改接受的答案吗?接受后我不能删除我的。

标签: wpf textbox command


【解决方案1】:

我知道我迟到了,但我得到了这个为我工作。尝试使用Key="Return" 而不是Key="Enter"

这是完整的例子

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

一定要在你的绑定中使用UpdateSourceTrigger=PropertyChanged,否则属性不会更新直到失去焦点,按回车也不会失去焦点...

希望这对您有所帮助!

【讨论】:

  • 它必须被接受为解决方案,而不是接受糟糕的答案RegisterAttached
  • @Skynet094 类似这样:AddCommand= new Command(ExecuteAdd); 然后定义ExecuteAdd 方法
【解决方案2】:

您可能没有将命令设为属性,而是设为字段。它仅适用于绑定到属性。将您的 AddCommand 更改为一个属性,它将起作用。 (您的 XAML 使用属性而不是命令字段对我来说很好 -> 不需要任何代码!)

【讨论】:

  • 同意。 &lt;KeyBinding Command="{Binding Path=AddCommand}" Key="Enter"/&gt; 对我也很有效!
【解决方案3】:

这是我为此创建的附加依赖项属性。它的优点是确保您的文本绑定在命令触发之前更新回 ViewModel(对于不支持属性更改更新源触发器的 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】:

    您需要定义 Gesture 而不是 KeyBinding 的 Key 属性:

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

    【讨论】:

      【解决方案5】:

      除了Mark Heath的回答之外,我通过这种方式实现命令参数附加属性,使课程更进一步;

      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"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-15
        • 2014-04-20
        相关资源
        最近更新 更多