5

为什么我的 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 有什么问题?

编辑

我发现这个 SO answer建议制作UserControl.IsTabStop="False"并将它的 Child 控件的 TabIndex 绑定到UserControl.TabIndex,这部分有效。

我的 TabOrder 现在是

  • 搜索组合框
  • 搜索文本
  • 搜索按钮
  • 数据库组合框
  • 保存按钮
  • 取消按钮
  • 内容控制
4

3 回答 3

3

Apparently by default, WPF reads all the controls, inside and outside UserControls, at the same tab level (unless specified otherwise). Since the controls inside the UserControl do not have a TabIndex specified, they get tabbed to last after the first tab cycle.

The workaround was to bind the TabIndex of the inner controls to the TabIndex of the UserControl

<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>

The only thing special about my SearchView is that the controls all set

TabIndex="{Binding Path=TabIndex, RelativeSource={RelativeSource 
    AncestorType={x:Type local:SearchView}}}"

Tab Order goes:

  • UserControl Search ComboBox
  • UserControl Search Text
  • UserControl Search Button
  • Database ComboBox
  • ContentControl
  • Save Button
  • Cancel Button
于 2011-10-10T16:02:38.500 回答
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>
于 2011-10-10T15:51:39.157 回答
0

尝试在您的父 DockPanel 中包含 KeyboardNavigation.TabNavigation="Local"。

<DockPanel KeyboardNavigation.TabNavigation="Local">   

键盘导航模式

于 2011-10-10T15:30:08.427 回答