none
键盘按键问题求助 RRS feed

  • 问题

  • 在WPF程序中,用以下代码处理输入框的回车事件,实现tab效果。

    FocusNavigationDirection focusDirection = FocusNavigationDirection.Next;
                    TraversalRequest request = new TraversalRequest(focusDirection);
                    UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
                    if (elementWithFocus != null)
                    {
                        elementWithFocus.MoveFocus(request);
                    }

    但是在某一个输入框回车事件中调用了另外一个dll窗体(该dll无法更改),里面也有回车事件,导致在我的界面中输入框的回车事件触发了两次,跳过了一个输入框,这个问题该如何处理?
    2013年10月2日 2:37

答案

  • Hi,

      欢迎来到MSDN中文论坛。

      你应该单独对回车以及Tab键进行处理。

     private void Grid1_KeyUp(object sender, KeyEventArgs e)
            {
                UIElement element = Keyboard.FocusedElement as UIElement;
                if (e.Key == Key.Down || e.Key == Key.Enter)
                {                            
                    if (element != null)
                    {
                        element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                    }
                }
                if (e.Key == Key.Up)
                {
                    if (element != null)
                    {
                        element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Previous));
                    }
                }
            }
     
    


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2013年10月9日 8:06
    版主