Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does z-index affect tab order?

Tags:

c#

wpf

xaml

this is my first question, so sorry if I've not done something correctly.

I can find nothing on the internet regarding this topic:

When I create a new wpf app with the following Grid, tab order is as expected (first textbox, down):

<Grid>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="95,95,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" />
    <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="95,160,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="textBox2" HorizontalAlignment="Left" Height="23" Margin="95,217,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Grid>

If I set Panel.ZIndex on one of them, it can affect the tab index in different ways (depending on which one). This one is the most notable, since it seems to reverse the tab order:

<Grid>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="95,95,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" />
    <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="95,160,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" Panel.ZIndex="2"/>
    <TextBox x:Name="textBox2" HorizontalAlignment="Left" Height="23" Margin="95,217,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Grid>

So how does this work? Does zindex really affect the tab order, or does it somehow create a new tab group to hold the controls with the same zindex? Is there a way to stop this so zindex has no impact at all?

I would prefer a solution that doesn't involve hard coding the tab order using the TabIndex property. Ideally there would be some way to prevent the zindex interfering

like image 709
Téigan P Avatar asked Oct 20 '25 01:10

Téigan P


1 Answers

If you do not set the _TabIndex_es, ZIndex matters. This is because KeyboardNavigation uses VisualTreeHelper.GetChild() to find the next child (See GetNextSibling(...) in KeyboardNavigation source code) and this method returns visuals with lower _ZIndex_es first. If you hard code the TabIndex everything is as expected.

like image 151
Ron Avatar answered Oct 22 '25 16:10

Ron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!