如何将KeyUp / down绑定到按钮Click事件?

时间:2010-08-27 17:54:46

标签: silverlight silverlight-4.0

假设我在此控件中有textbox,combobox,button,...的usercontrol。

Button1绑定到视图模型中的ICommand。

我的要求是:当用户在任何字段中按Enter键时,如任何文本框,组合框,它将触发Button1 Click事件,以便调用ICommand。

如何实现这个?

2 个答案:

答案 0 :(得分:3)

我为这种情况创建了简单的行为

<TextBox Grid.Row="2" x:Name="Tags">
    <i:Interaction.Behaviors>
       <this:KeyEnterCommand Command="{Binding AddTagsCommand}" CommandParameter="{Binding ElementName=Tags}" />
    </i:Interaction.Behaviors>
</TextBox>

行为代码:

public class KeyEnterCommand : Behavior<Control>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.KeyDown += KeyDown;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.KeyDown -= KeyDown;
    }

    void KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (e.Key == System.Windows.Input.Key.Enter && Command != null)
        {
            Command.Execute(CommandParameter);
        }
    }

    #region Command (DependencyProperty)

    /// <summary>
    /// Command
    /// </summary>
    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }
    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register("Command", typeof(ICommand), typeof(KeyEnterCommand),
            new PropertyMetadata(null));

    #endregion

    #region CommandParameter (DependencyProperty)

    /// <summary>
    /// CommandParameter
    /// </summary>
    public object CommandParameter
    {
        get { return (object)GetValue(CommandParameterProperty); }
        set { SetValue(CommandParameterProperty, value); }
    }
    public static readonly DependencyProperty CommandParameterProperty =
        DependencyProperty.Register("CommandParameter", typeof(object), typeof(KeyEnterCommand),
            new PropertyMetadata(null));


    #endregion
}

答案 1 :(得分:0)

向UserControl添加依赖项属性: -

public ICommand EnterKeyCommand
{
    get { return GetValue(EnterKeyCommandProperty) as ICommand; }
    set { SetValue(EnterKeyCommandProperty, value); }
}

public static readonly DependencyProperty EnterKeyCommandProperty =
        DependencyProperty.Register(
                "EnterKeyCommand",
                typeof(ICommand),
                typeof(MyControl),
                null);

使用AddHandler方法在UserControl上附加Keyup事件的处理程序: -

void MyControl()
{

    InitializeComponent();
    this.AddHandler(UIElement.KeyUpEvent, new KeyEventHandler(UserControl_KeyUp), true);  //Note that last parameter important
}

void UserControl_KeyUp(object sender, KeyEventArgs e)
{
   if (e.Key == Key.Enter && EnterKeyCommand != null && EnterKeyCommand.CanExecute(null))
   {
       EnterKeyCommand.Execute(null);
   }

}

请注意,使用AddHandler可以拦截已经处理过的事件。

另请注意,为简化起见,这是简化的。实际上,您还希望为Command参数实现另一个依赖项属性,并将其传递给CanExecuteExecute而不是null。您还需要检测OriginalSourceTextBox是否为AcceptsReturn设置为真。