Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF TabIndex in a composite control

I have a simple window with a simple composite control embedded within it.

(Main Window)

<Window x:Class="TabOrder.Window1"
xmlns:local="clr-namespace:TabOrder"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
    <Label HorizontalAlignment="Left" VerticalAlignment="Top">First</Label>
    <TextBox TabIndex="0" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,0,0,0"/>

    <Label HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0">Second</Label>
    <TextBox TabIndex="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,30,0,0"/>

    <local:MyControl Margin="0,60,0,0" VerticalAlignment="Top" HorizontalAlignment="Stretch" TabIndex="2"/>
</Grid>

(Composite control)

<UserControl x:Class="TabOrder.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
    <Label HorizontalAlignment="Left" VerticalAlignment="Top">Third</Label>
    <TextBox HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,0,0,0"/>

    <Label HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0">Fourth</Label>
    <TextBox HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,30,0,0"/>
</Grid>

As expected on my form I get 4 text boxes...

  • First
  • Second
  • Third
  • Fourth

But when "First" has focus and I hit tab the focus is switched to "Third". WPF seems to be seeing the tab list as a single flat list rather than as a tree where MyControl is TabIndex 3 and the text box "Third" the first tabbed control within it.

Is this a bug in WPF or is there another way of doing this? The composite control is used in many windows, it could even be used more than once on a single window.

like image 211
Peter Morris Avatar asked Dec 13 '22 01:12

Peter Morris


1 Answers

I know this response is quite late... but have you tried:

<UserControl ... KeyboardNavigation.TabNavigation="Local">

Doing so will ensure once your UserControl has recieved focus, you will navigate only through TabStop within your UserControl (instead of worring about conflicting TabIndex values throughout your app). After looping through the TabStops of your UserControl, TabNavigation will resume to the TabStop outside of it.

http://msdn.microsoft.com/en-us/library/system.windows.input.keyboardnavigationmode.aspx

like image 185
Scott Avatar answered Dec 16 '22 16:12

Scott