DatePicker - Move Focus On Enter Key

Oct 24, 2009 at 2:22 PM
Edited Oct 24, 2009 at 3:33 PM

Hello,

how can I make the focus move to the next control when the DatePicker textbox has the focus and the user presses the Enter key?

The KeyDown event is not raised by DatePicker when the user presses the Enter key.

As a workaround I changed the source code of DatePicker as shown below. 

However, this does not work. The MoveFocus command is executed but the focus does not move. Nothing happens.

Does anybody have an idea?

Thanks for your time.

        private bool ProcessDatePickerKey(KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.System:
                {
                    switch (e.SystemKey)
                    {
                        case Key.Down:
                        {
                            if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
                            {
                                TogglePopUp();
                                return true;
                            }

                            break;
                        }
                    }

                    break;
                }

                case Key.Enter:
                {
                    this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));     
                    //SetSelectedDate();
                    return true;
                }
            }
        private bool ProcessDatePickerKey(KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.System:
                {
                    switch (e.SystemKey)
                    {
                        case Key.Down:
                        {
                            if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
                            {
                                TogglePopUp();
                                return true;
                            }
                            break;
                        }
                    }
                    break;
                }
                case Key.Enter:
                {
                    this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));     
                    //SetSelectedDate();
                    return true;
                }
     
Oct 24, 2009 at 3:37 PM
Edited Oct 24, 2009 at 3:40 PM

Finally, I could solve this problem. I added the following lines to DatePicker.cs:

        public event EventHandler<KeyEventArgs> EnterKeyPressed;

        protected virtual void OnEnterKeyPressed(KeyEventArgs e)
        {
            EventHandler<KeyEventArgs> handler = this.EnterKeyPressed;
            if (null != handler)
            {
                handler(this, e);
            }
        }

        private bool ProcessDatePickerKey(KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.System:
                {
                    switch (e.SystemKey)
                    {
                        case Key.Down:
                        {
                            if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
                            {
                                TogglePopUp();
                                return true;
                            }

                            break;
                        }
                    }

                    break;
                }

                case Key.Enter:
                {
                    OnEnterKeyPressed(e);

                    //SetSelectedDate();
                    return true;
                }
            }

Then, in my page/window code behind, I handle the new event as follows:

        private void DatePicker_EnterKeyPressed(object sender, KeyEventArgs e)
        {
            var uie = e.OriginalSource as UIElement;

            if (e.Key == Key.Enter)
            {
                e.Handled = true;
                uie.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            }
        }

 Now the focus changes to the next control when I press the Enter key.