How to programmatically navigate WPF UI element tab stops?

前端 未结 2 828
温柔的废话
温柔的废话 2020-12-06 06:16

Can anyone tell me how to programmatically navigate through all UI element tab stops in a WPF application? I want to start with the first tab stop sniff the corresponding e

2条回答
  •  自闭症患者
    2020-12-06 07:17

    You do that using MoveFocus as shown in this MSDN article which explains everything about focus: Focus Overview.

    Here is some sample code to get to the next focused element (got it from that article, slightly modified).

    // MoveFocus takes a TraversalRequest as its argument.
    TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
    
    // Gets the element with keyboard focus.
    UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
    
    // Change keyboard focus.
    if (elementWithFocus != null) 
    {
        elementWithFocus.MoveFocus(request);
    }
    

提交回复
热议问题