0
votes

In the default TabControl you can switch tabs using Ctrl+Tab, however if one of the tabs contain DataGrid it all breaks. For example, if I had a simple TabControl containing nothing but TextBoxes:

<TabControl>
    <TabItem Header="Tab1">
        <TextBox/>
    </TabItem>
    <TabItem Header="Tab2">
        <TextBox/>
    </TabItem>
    <TabItem Header="Tab3">
        <TextBox/>
    </TabItem>
</TabControl>

And repeatedly pressed Ctrl+Tab, current Tab would cycle:

Tab1 > Tab2 > Tab3 > Tab1 > ...etc

but if I were to put virtually any DataGrid in one of the middle Tabs, for example:

...    
<TabItem Header="Tab2"> <!--replacing 2nd tab content-->
    <TabItem.Resources>
        <x:Array x:Key="Items" Type="{x:Type Run}">
            <Run Text="Foo"/>
            <Run Text="Bar"/>
            <Run Text="Baz"/>
        </x:Array>
    </TabItem.Resources>
    <DataGrid ItemsSource="{StaticResource Items}"/>
</TabItem>

the Ctrl+Tab would get stuck not being able to ever get past said Tab:

Tab1 > Tab2 > Tab1 > Tab2 > ...etc

How can I fix the issue?


I have tried IsTabStop="False" on the DataGrid and several combinations of KeyBoardNavigation properties, all of those had either no effect or were contraproductive.


Edit(2): the issue seems to appear mainly when the DataGrid is the first focusable child of TabItem, for example content:

<StackPanel>
    <TextBox/>
    <DataGrid ItemsSource="{StaticResource Items}"/>
</StackPanel>

almost works, until user selects the DataGrid. If the grid is selected next Ctrl+Tab goes back to first Tab rather than to the next one.

2
Not sure what are you talking about, ctrl/tab works fine at my end with a DG in one of the tabs, something else might be causing that! - SamTh3D3v
@SamTheDev I am using empty 4.7.2 project running on Windows 10 machine, with nothing else but XAML code included in the question (pasted as the Window content), what is your setup? - wondra
Same I guess, just pasted your code, three tabs where the third one is the one with the grid, .net 4.7.2 on a win 10 1903. - SamTh3D3v
@SamTheDev try putting the grid in second tab. The cycle stops/restarts at first grid encountered - if the grid is in the last tab you would not be able to tell if it stopped there or the cycle completed as normal. - wondra
Hmmm interesting, now I see the problem! - SamTh3D3v

2 Answers

1
votes

Looks like it's a focus problem, when the Datagrid gets the focus it's lost from the TabControl, so set that focus programmatically whenever the ctrl/tab is pressed where the DataGrid is:

 <TabControl  x:Name="TabCtrl">
        <TabItem Header="Tab1" x:Name="Tab1" >
            <TextBox/>
        </TabItem>
        <TabItem Header="Tab3" x:Name="Tab2"  PreviewKeyDown="UIElement_OnPreviewKeyDown">
            <TabItem.Resources>
                <x:Array x:Key="Items" Type="{x:Type Run}">
                    <Run Text="Foo"/>
                    <Run Text="Bar"/>
                    <Run Text="Baz"/>
                </x:Array>
            </TabItem.Resources>
            <DataGrid ItemsSource="{StaticResource Items}" />
        </TabItem>
        <TabItem Header="Tab2"  x:Name="Tab3">
            <TextBox/>
        </TabItem>
    </TabControl>

The handler:

 private void UIElement_OnPreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Tab &&
            (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control && sender is TabItem)
        {
            TabCtrl.Focus();
        }
    }
0
votes

Have you tried

IsTabStop="False"

on your controls. This prevents focus into that control? A user could still manually click on the grid for focus (or other control), but without, it will ignore trying to get focus to that control.

Just to expand on the IsTabStop, you may want to try wrapping it into a user control and stop at the user control so it does not go into anything WITHIN it... such as

<UserControl IsTabStop="false">
   <DataGrid>
      …
   </DataGrid>
</UserControl>