WPF tab not working

I have a view in WPF that I'm fighting for the correct tab settings. I have three text fields (allows you to call them Text1, Text2 and Text3) and two custom controls, each of which has several other text fields and different controls (allows you to call them Custom1 and Custom2).

The layout is such that the tab stream should go Text1, Text2, Custom1, Custom2, Text3. I set the TabIndex property for each control to match this order and verified that they are all set to IsTabStop.

The problem is that the actual tab stream goes Text1, Text2, Text3, and then Custom1, Custom2, and I cannot understand why. When it goes to user controls, it correctly steps over each control in them, as I expected. I just can't understand why it goes to the third text box before it goes to the first custom control.

I tried everything I can think of, including making sure that all xaml elements are arranged in tab order, but nothing helps.

I suspect that it is sorting through all its main controls before paying attention to any user controls, but I'm not in ideas. Any help would be great.

EDIT: Here is my xaml:

<Grid> <GroupBox x:Name="_groupBox" BorderBrush="Transparent" BorderThickness="0"> <Grid x:Name="_card"> <Label Content="A:" Height="28" HorizontalAlignment="Left" Margin="5,3,0,0" Name="_labelA" VerticalAlignment="Top" /> <Label Content="B:" Height="28" HorizontalAlignment="Left" Margin="5,25,0,0" Name="_labelB" VerticalAlignment="Top" /> <TextBox Name="_a" Height="20" HorizontalAlignment="Left" Text="{Binding AText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding AEnabled}" Margin="94,5,0,0" VerticalAlignment="Top" LostFocus="InputNameLeave" Width="221" TabIndex="0" /> <TextBox Name="_b" Height="20" HorizontalAlignment="Left" Margin="94,26,0,0" Text="{Binding BText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="102" TabIndex="1" /> <my:CustomControlA HorizontalAlignment="Left" Margin="-6,55,0,0" x:Name="_custom1" VerticalAlignment="Top" TabIndex="2" IsTabStop="True" /> <my:CustomControlB HorizontalAlignment="Left" Margin="334,0,0,0" x:Name="_custom2" VerticalAlignment="Top" Width="320" TabIndex="3" IsTabStop="True" /> <Label Content="C:" Height="28" HorizontalAlignment="Left" Margin="342,59,0,0" Name="_labelC" VerticalAlignment="Top" /> <TextBox Name="_c" Height="20" HorizontalAlignment="Left" Margin="417,60,0,0" Text="{Binding CText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding CEnabled}" VerticalAlignment="Top" Width="154" TabIndex="4" /> </Grid> </GroupBox> </Grid> 
+4
source share
2 answers

I don't have a custom control, so I created one that contained only a TextBox. After connecting to your XAML, I found that the order of the tabs is as follows:

TextBox1, TextBox2, CustomControl1, CustomControl2, TextBox3, TextBox in CustomControl1, TextBox in CustomControl2.

Note that after the TextBox2 tab shifts focus to user controls, not to any child controls that they have. The TextBox in my custom control did not have TabIndex, so its TabIndex was considered the default value, Int32.MaxValue (see the MSDN documentation for the TabIndex property ). Therefore, they end in tab order.

I found that everything works better if I flagged the user controls as IsTabStop="False" (I don’t understand why I would like to focus on the custom controls themselves) and set the TextBox tab index to the custom control specified in the control <my:CustomControl /> by adding the TabIndex="{TemplateBinding TabIndex}" attribute to the TextBox in the custom control. As soon as I did this, the order of the tabs was, as expected,

TextBox1, TextBox2, TextBox in CustomControl1, TextBox in CustomControl2, TextBox3.

Of course, my user control consists of only one TextBox, so for me fixed things with a fixed index are fixed. I do not have your code, so I can’t say for sure, but this may be enough to set the tab-index of all the child controls in your custom controls in the same way in the <my:CustomControl /> element.

EDIT : if you cannot use TemplateBinding and instead have a .xaml file with the corresponding .xaml.cs file, then you have what is called a user control, not user control. In this case, you can try setting the tab index inside the XAML for CustomControlA using something like the following:

 TabIndex="{Binding Path=TabIndex, RelativeSource={RelativeSource AncestorType={x:Type my:CustomControlA}}}" 
+10
source

Please post your XAML code. At the same time, some things to consider include:

  • Check the value of the KeyboardNavigation.TabIndex attribute again
  • Do you dynamically add custom tabs at runtime?
  • Do you programmatically modify or interact with user controls at runtime?
  • Make sure the tabs are listed in the correct order in the XAML markup
+1
source

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


All Articles