Focus and TabIndex on UserControls

I have a strange behavior: I have a MainWindow containing text fields and (simple) usercontrols (text field and button), but I only split this into a text field for debugging purposes.

When I use text fields and custom elements WITHOUT setting the TabIndex property, the cursor moves through the controls in the correct order (in the order that the controls were added to the window)

When I use text fields and WITH user controls by setting the TabIndex property, the cursor moves through the controls in the wrong order (first all user controls and then all text fields), this is also true when the TabIndex value is set to the value corresponding to the order in which the control was added

Here is my usercontrol

<UserControl x:Class="SmallControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" > <TextBox x:Name="txTEXT" Text="{Binding Text}" /> </UserControl> 

The following Mainwindow xaml file results in order 000000,111111,222222,333333, thats ok

  <GroupBox Header="Small,Textbox,Small,TextBox without TabIndex"> <UniformGrid Columns="4"> <local:SmallControl Text="000000" /> <TextBox Text="111111" /> <local:SmallControl Text="222222" /> <TextBox Text="333333" /> </UniformGrid> </GroupBox> 

The following Mainwindow xaml file leads to the order 000000,222222,111111,333333, thats NOT ok

  <GroupBox Header="Small,Textbox,Small,TextBox with TabIndex"> <UniformGrid Columns="4"> <local:SmallControl TabIndex="0" Text="000000" /> <TextBox TabIndex="1" Text="111111" /> <local:SmallControl TabIndex="2" Text="222222" /> <TextBox TabIndex="3" Text="333333" /> </UniformGrid> </GroupBox> 

Is there a way to use TabIndex without having to add controls in the "correct" order in xaml?

Relations Klaus

+4
source share
1 answer

By default, WPF reads all controls, both inside and outside of UserControls, at the same tab level (unless otherwise specified). Since the controls inside UserControl do not have the specified TabIndex pointer, they get the tab to the last after the first tab loop.

To change this behavior, I usually set IsTabStop="False" in my UserControl definition, then bind the internal TabIndex controls to the UserControl TabIndex

UserControl XAML

 <TextBox x:Name="txTEXT" Text="{Binding Text}" TabIndex="{Binding Path=TabIndex, RelativeSource={RelativeSource AncestorType={x:Type local:SearchView}}}"/> 

Using XAML

 <GroupBox Header="Small,Textbox,Small,TextBox with TabIndex"> <UniformGrid Columns="4"> <local:SmallControl TabIndex="0" Text="000000" IsTabStop="False" /> <TextBox TabIndex="1" Text="111111" /> <local:SmallControl TabIndex="2" Text="222222" IsTabStop="False" /> <TextBox TabIndex="3" Text="333333" /> </UniformGrid> </GroupBox> 

You may also be able to translate it correctly by setting KeyboardNavigation.TabNavigation in your UserControl to Local . I seem to remember that I had problems with this, but I honestly can't remember the details, so this might work.

 <UserControl x:Class="SmallControl" ... KeyboardNavigation.TabNavigation="Local" /> 
+24
source

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


All Articles