Tuesday, January 15, 2013

Framework Element Enter Key Press Event

Add two class your project

public class TextBoxEnterKeyTrigger : TriggerBase<UIElement>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
            TextBox textBox = this.AssociatedObject as TextBox;

            if (textBox != null)
            {
                this.AssociatedObject.KeyUp += new System.Windows.Input.KeyEventHandler(AssociatedObject_KeyUp);
            }
            else
            {
                throw new InvalidOperationException("This behavior only works with TextBoxes");
            }
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.KeyUp -= new KeyEventHandler(AssociatedObject_KeyUp);
        }

        private void AssociatedObject_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                TextBox textBox = AssociatedObject as TextBox;

                //This checks for an mvvm style binding and updates the source before invoking the actions.
                BindingExpression expression = textBox.GetBindingExpression(TextBox.TextProperty);
                if (expression != null)
                    expression.UpdateSource();

                InvokeActions(textBox.Text);
            }
        }
    }

    public class ExecuteCommandAction : TriggerAction<DependencyObject>
    {
        #region Properties

        public ICommand Command
        {
            get { return (ICommand)base.GetValue(CommandProperty); }
            set { base.SetValue(CommandProperty, value); }
        }

        public static ICommand GetCommand(DependencyObject obj)
        {
            return (ICommand)obj.GetValue(CommandProperty);
        }

        public static void SetCommand(DependencyObject obj, ICommand value)
        {
            obj.SetValue(CommandProperty, value);
        }

        // We use a DependencyProperty so we can bind commands directly rather
        // than have to use reflection info to find them
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(ExecuteCommandAction), null);
        #endregion Properties

        protected override void Invoke(object parameter)
        {
            ICommand command = Command ?? GetCommand(AssociatedObject);
            if (command != null && command.CanExecute(parameter))
            {
                command.Execute(parameter);
            }
        }
    }

and used this way


<TextBox
                                  Text="{Binding Path=text, Mode=TwoWay,ValidatesOnExceptions=True, NotifyOnValidationError=True}">
                        <i:Interaction.Triggers>
                            <lib:TextBoxEnterKeyTrigger>
                                <lib:ExecuteCommandAction Command="{Binding Path=Command,Mode=TwoWay}"/>
                            </lib:TextBoxEnterKeyTrigger>
                        </i:Interaction.Triggers>
                    </TextBox>



No comments:

Post a Comment