未遵循指定的 Tab 键顺序?

发布于 2024-12-08 19:43:07 字数 1552 浏览 0 评论 0原文

为什么我的 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 应该去

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

但是相反,它是

  • 搜索组合框
  • 搜索文本
  • 搜索按钮
  • ContentControl
  • 数据库组合框
  • 保存按钮
  • 取消按钮

我的TabOrder有什么问题吗?

编辑

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

我的 TabOrder 现在是

  • 搜索组合框
  • 搜索文本
  • 搜索按钮
  • 数据库组合框
  • 保存按钮
  • 取消按钮
  • ContentControl

Why isn't my XAML following the TabOrder I specified?

I currently have:

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

enter image description here

My TabOrder should go

  • Search ComboBox
  • Search Text
  • Search Button
  • Database ComboBox
  • ContentControl
  • Save Button
  • Cancel Button

But instead it goes

  • Search ComboBox
  • Search Text
  • Search Button
  • ContentControl
  • Database ComboBox
  • Save Button
  • Cancel Button

What do I have wrong with my TabOrder?

Edit

I found this SO answer which suggested making UserControl.IsTabStop="False", and binding it's Child control's TabIndex to UserControl.TabIndex, which partially works.

My TabOrder is now

  • Search ComboBox
  • Search Text
  • Search Button
  • Database ComboBox
  • Save Button
  • Cancel Button
  • ContentControl

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫轩蝶泪 2024-12-15 19:43:07

显然,默认情况下,WPF 在同一选项卡级别读取用户控件内部和外部的所有控件(除非另有指定)。由于 UserControl 内的控件没有指定 TabIndex,因此它们会在第一个 Tab 循环后切换到最后一个。

解决方法是将内部控件的 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}}}"

选项卡顺序如下:

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

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
空心空情空意 2024-12-15 19:43:07

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

<DockPanel KeyboardNavigation.TabNavigation="Local">   

键盘导航模式

Try including KeyboardNavigation.TabNavigation="Local" in your parent DockPanel.

<DockPanel KeyboardNavigation.TabNavigation="Local">   

KeyboardNavigationMode

物价感观 2024-12-15 19:43:07

这对我有用:

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

This works for me:

 <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文