为什么我的XAML不在我指定的TabOrder之后?
我目前有:
<DockPanel>
<Grid DockPanel.Dock="Top">
<UserControl TabIndex="0">
<StackPanel Orientation="Horizontal">
<ComboBox />
<TextBox Text="Search Text" />
<Button Content="Search" />
</StackPanel>
</UserControl>
<ComboBox TabIndex="1" />
<Separator />
<TextBox TabIndex="3" Text="Save" />
<TextBox TabIndex="4" Text="Cancel" />
</Grid>
<Grid>
<ContentControl TabIndex="2" />
<Popup />
</Grid>
</DockPanel>
我的TabOrder应该
但相反它
我的TabOrder有什么问题?
修改
我发现this SO answer建议制作UserControl.IsTabStop="False"
,并将其Child控件的TabIndex绑定到UserControl.TabIndex
,这部分有效。
我的TabOrder现在是
答案 0 :(得分:2)
显然,默认情况下,WPF会在同一选项卡级别读取UserControls内部和外部的所有控件(除非另有说明)。由于UserControl中的控件没有指定TabIndex,因此它们会在第一个制表符循环后标记为最后一个。
解决方法是将内部控件的TabIndex
绑定到UserControl的TabIndex
<DockPanel Margin="10" KeyboardNavigation.TabNavigation="Cycle">
<Grid DockPanel.Dock="Top"
local:GridProperties.ColumnCount="6"
local:GridProperties.StarColumns="0">
<TextBlock Text="Header" FontSize="20" FontWeight="Bold" />
<ContentControl Grid.Column="1" TabIndex="0" IsTabStop="False" Content="{Binding SearchViewModel}" />
<ComboBox Grid.Column="2" Margin="5" Width="100" />
<Separator Grid.Column="3" Style="{StaticResource VerticalSeparatorStyle}" />
<Button Grid.Column="4" TabIndex="3" Content="Save" Width="75" Margin="5" />
<Button Grid.Column="5" TabIndex="4" Content="Cancel" Width="75" Margin="5" />
</Grid>
<Line HorizontalAlignment="Stretch" X2="1" Stretch="Fill" Stroke="Black" StrokeThickness="1" Margin="0,5" DockPanel.Dock="Top" />
<Grid x:Name="ShellContentRoot">
<!-- Current Page -->
<ContentControl TabIndex="2" Content="{Binding CurrentAccount}" IsTabStop="False" />
<!-- Search Results -->
<local:PopupPanel local:PopupPanel.PopupParent="{Binding ElementName=ShellContentRoot}" />
</Grid>
</DockPanel>
我的SearchView
唯一特别之处在于控件全部设置
TabIndex="{Binding Path=TabIndex, RelativeSource={RelativeSource
AncestorType={x:Type local:SearchView}}}"
Tab顺序:
答案 1 :(得分:0)
尝试在您的父DockPanel中包含KeyboardNavigation.TabNavigation =“Local”。
<DockPanel KeyboardNavigation.TabNavigation="Local">
答案 2 :(得分:0)
这对我有用:
<DockPanel >
<DockPanel DockPanel.Dock="Top">
<UserControl TabIndex="0" KeyboardNavigation.TabNavigation="Local" DockPanel.Dock="Left">
<StackPanel Orientation="Horizontal">
<ComboBox />
<TextBox Text="Search Text" />
<Button Content="Search" />
</StackPanel>
</UserControl>
<ComboBox TabIndex="1" DockPanel.Dock="Left" />
<Separator />
<TextBox TabIndex="3" Text="Save" DockPanel.Dock="Left"/>
<TextBox TabIndex="4" Text="Cancel" DockPanel.Dock="Left"/>
</DockPanel>
<Grid DockPanel.Dock="Bottom">
<ContentControl TabIndex="2" Height="100" Width="100"/>
<Popup />
</Grid>
</DockPanel>