Shift+Tab not working in TreeView control

前端 未结 2 580
悲&欢浪女
悲&欢浪女 2020-12-17 03:46

I cannot get backwards navigation using Shift+Tab to work in a TreeView that contains TextBoxs, forward navigation using Tab works fine and jump from TextBox to TextBox insi

相关标签:
2条回答
  • 2020-12-17 04:44

    You don't have to use a custom class inherited from TreeView:

    treeView.PreviewKeyDown += this.HandleTreeView_PreviewKeyDown
    

    together with:

    private void HandleTreeView_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Shift)
            && e.Key == Key.Tab)
        {
            var focusedElement = Keyboard.FocusedElement;
            if (focusedElement != null)
            {
                focusedElement.MoveFocus(FocusNavigationDirection.Previous, 1);
            }
    
            e.Handled = true;
        }
    }
    

    Also works fine.

    With this solution you could, for example, create a custom behavior and attach it to your TreeView.

    0 讨论(0)
  • 2020-12-17 04:47

    If you look in the TreeView.OnKeyDown handler using ILSpy/Reflector, you can see the cause of your issues. The TreeView has special handling when Shift+Tab is pressed. The relevant code is:

    Key key = e.Key;
    if (key != Key.Tab) {
        // ... 
    }
    else {
        if (TreeView.IsShiftKeyDown && base.IsKeyboardFocusWithin &&
            this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Previous))) {
            e.Handled = true;
            return;
        }
    }
    

    Unfortunately, you'd need to use a custom TreeView class to work around this. Something like this works:

    public class MyTreeView : TreeView {
        protected override void OnKeyDown(KeyEventArgs e) {
            if ((Keyboard.Modifiers & ModifierKeys.Shift) != 0 && e.Key == Key.Tab)
                return;
    
            base.OnKeyDown(e);
        }
    }
    
    0 讨论(0)
提交回复
热议问题