Shift + Tab does not work in TreeView control

I can’t get back navigation with Shift + Tab to work in a TreeView that contains a TextBox, redirecting navigation using a tab works fine and go from TextBox to TextBox inside TreeView. At any time, Shift + Tab is used when one of the TextBox is inside the TreeView, and then focus moves to the previous control outside the TreeView instead of the previous TreeView control.

Also, its only navigation bar, Shift + Tab, does not work correctly, Ctrl + Shift + Tab works as expected and in the correct order.

Any suggestions on what I'm doing wrong?

Code example:

<Window x:Class="TestTabTreeView.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <Style TargetType="TreeViewItem"> <Setter Property="KeyboardNavigation.TabNavigation" Value="Continue" /> </Style> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <TextBox Text="First Line" Grid.Row="0" /> <TreeView Grid.Row="1" KeyboardNavigation.TabNavigation="Continue" IsTabStop="False"> <TreeViewItem IsExpanded="True"><TreeViewItem.Header><TextBox Text="Popular Words"/></TreeViewItem.Header> <TreeViewItem><TreeViewItem.Header><TextBox Text="Foo"/></TreeViewItem.Header></TreeViewItem> <TreeViewItem><TreeViewItem.Header><TextBox Text="Bar"/></TreeViewItem.Header></TreeViewItem> <TreeViewItem><TreeViewItem.Header><TextBox Text="Hello"/></TreeViewItem.Header></TreeViewItem> </TreeViewItem> <TreeViewItem IsExpanded="True"><TreeViewItem.Header><TextBox Text="Unpopular Words"/></TreeViewItem.Header> <TreeViewItem><TreeViewItem.Header><TextBox Text="Work"/></TreeViewItem.Header></TreeViewItem> <TreeViewItem><TreeViewItem.Header><TextBox Text="Duplication"/></TreeViewItem.Header></TreeViewItem> </TreeViewItem> </TreeView> <TextBox Text="Last Line" Grid.Row="2" /> </Grid> 

+4
source share
2 answers

If you look in the TreeView.OnKeyDown handler using ILSpy / Reflector, you can see the cause of your problems. TreeView has special handling when you press Shift + Tab. Relevant Code:

 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, for this you need to use your own TreeView class. 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); } } 
+7
source

You do not need to use your own 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 great.

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

0
source

Source: https://habr.com/ru/post/1311063/


All Articles