WPF Tab 键顺序工作错误

发布于 2024-10-30 09:45:23 字数 2235 浏览 3 评论 0原文

我在 WPF 中有一个观点,我一直在努力使 Tab 键顺序正确。我有三个文本框(我们称它们为 Text1、Text2 和 Text3)和两个自定义控件,每个控件上都有几个其他文本框和各种控件(我们称它们为 Custom1 和 Custom2)。

布局是这样的,选项卡流应为 Text1、Text2、Custom1、Custom2、Text3。我已在每个控件上设置 TabIndex 属性以匹配此顺序,并验证所有控件均设置为 IsTabStop。

问题是,实际的选项卡流程是 Text1、Text2、Text3,然后是 Custom1、Custom2,我不明白为什么。当它进入自定义控件时,它确实按照我的预期正确地跳过了其中的每个控件。我只是不明白为什么它在进入第一个自定义控件之前进入第三个文本框。

我已经尝试了我能想到的一切,包括确保所有 xaml 元素都按 Tab 键顺序排列,但似乎没有任何帮助。

我怀疑它在关注任何自定义控件之前会先检查所有基本控件,但我没有想法。任何帮助都会很棒。

编辑: 这是我的 xaml:

<Grid>
    <GroupBox x:Name="_groupBox" BorderBrush="Transparent" BorderThickness="0">
        <Grid x:Name="_card">
            <Label Content="A:" Height="28" HorizontalAlignment="Left" Margin="5,3,0,0" Name="_labelA" VerticalAlignment="Top" />
            <Label Content="B:" Height="28" HorizontalAlignment="Left" Margin="5,25,0,0" Name="_labelB" VerticalAlignment="Top" />
            <TextBox Name="_a" Height="20" HorizontalAlignment="Left" Text="{Binding AText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding AEnabled}" Margin="94,5,0,0"  VerticalAlignment="Top" LostFocus="InputNameLeave" Width="221" TabIndex="0" />
            <TextBox Name="_b" Height="20" HorizontalAlignment="Left" Margin="94,26,0,0" Text="{Binding BText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="102" TabIndex="1" />
            <my:CustomControlA HorizontalAlignment="Left" Margin="-6,55,0,0" x:Name="_custom1" VerticalAlignment="Top" TabIndex="2" IsTabStop="True" />
            <my:CustomControlB HorizontalAlignment="Left" Margin="334,0,0,0" x:Name="_custom2" VerticalAlignment="Top" Width="320" TabIndex="3" IsTabStop="True" />
            <Label Content="C:" Height="28" HorizontalAlignment="Left" Margin="342,59,0,0" Name="_labelC" VerticalAlignment="Top" />
            <TextBox Name="_c" Height="20" HorizontalAlignment="Left" Margin="417,60,0,0" Text="{Binding CText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding CEnabled}"  VerticalAlignment="Top" Width="154" TabIndex="4" />
        </Grid>
    </GroupBox>
</Grid>

I have a view in WPF that I have been fighting to get the tab order correct on. I have three text boxes (lets call them Text1, Text2, and Text3) and two custom controls, each of which has several other text boxes and assorted controls on them (lets call them Custom1 and Custom2).

The layout is such that tab flow should go Text1, Text2, Custom1, Custom2, Text3. I have set the TabIndex property on each control to match this ordering, and verified that all of them are set to IsTabStop.

The problem is, the actual tab flow goes Text1, Text2, Text3, and then Custom1, Custom2, and I cannot figure out why. When it goes to the custom controls, it does properly step over every control in them as I would expect. I just can't figure out why it goes to the third text box before it goes to the first custom control.

I have tried everything I can think of, including making sure all of the xaml elements are arranged in tab order, but nothing seems to help.

I suspect it is going over all of its basic controls before giving focus to any custom controls, but I am out of ideas. Any help would be great.

EDIT:
Here's my xaml:

<Grid>
    <GroupBox x:Name="_groupBox" BorderBrush="Transparent" BorderThickness="0">
        <Grid x:Name="_card">
            <Label Content="A:" Height="28" HorizontalAlignment="Left" Margin="5,3,0,0" Name="_labelA" VerticalAlignment="Top" />
            <Label Content="B:" Height="28" HorizontalAlignment="Left" Margin="5,25,0,0" Name="_labelB" VerticalAlignment="Top" />
            <TextBox Name="_a" Height="20" HorizontalAlignment="Left" Text="{Binding AText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding AEnabled}" Margin="94,5,0,0"  VerticalAlignment="Top" LostFocus="InputNameLeave" Width="221" TabIndex="0" />
            <TextBox Name="_b" Height="20" HorizontalAlignment="Left" Margin="94,26,0,0" Text="{Binding BText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="102" TabIndex="1" />
            <my:CustomControlA HorizontalAlignment="Left" Margin="-6,55,0,0" x:Name="_custom1" VerticalAlignment="Top" TabIndex="2" IsTabStop="True" />
            <my:CustomControlB HorizontalAlignment="Left" Margin="334,0,0,0" x:Name="_custom2" VerticalAlignment="Top" Width="320" TabIndex="3" IsTabStop="True" />
            <Label Content="C:" Height="28" HorizontalAlignment="Left" Margin="342,59,0,0" Name="_labelC" VerticalAlignment="Top" />
            <TextBox Name="_c" Height="20" HorizontalAlignment="Left" Margin="417,60,0,0" Text="{Binding CText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding CEnabled}"  VerticalAlignment="Top" Width="154" TabIndex="4" />
        </Grid>
    </GroupBox>
</Grid>

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

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

发布评论

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

评论(2

最美不过初阳 2024-11-06 09:45:23

我没有可用的自定义控件,因此我创建了一个仅包含文本框的控件。将其连接到 XAML 后,我发现 Tab 键顺序如下:

TextBox1、TextBox2、CustomControl1、CustomControl2、TextBox3、CustomControl1 内的 TextBox、CustomControl2 内的 TextBox。

请注意,在 TextBox2 选项卡将焦点移到自定义控件本身上,而不是它们碰巧拥有的任何子控件上。我的自定义控件中的 TextBox 没有 TabIndex,因此假定其 TabIndex 为默认值 Int32.MaxValue(请参阅 TabIndex 属性的 MSDN 文档)。因此它们在 Tab 键顺序中排在最后。

我发现如果将自定义控件标记为 IsTabStop="False" (我不明白为什么我希望将焦点放在自定义控件本身上),然后设置选项卡,效果会更好 -通过将属性 TabIndex="{TemplateBinding TabIndex}" 添加到自定义控件中的文本框。完成此操作后,正如我所期望的那样,Tab 键顺序

为 TextBox1、TextBox2、CustomControl1 内的 TextBox、CustomControl2 内的 TextBox、TextBox3。

当然,我的自定义控件仅包含一个 TextBox,因此设置单个选项卡索引可以解决我的问题。我没有您的代码,所以我不能肯定地说,但将自定义控件中所有子控件的选项卡索引设置为 中的选项卡索引可能就足够了; 元素以同样的方式。

编辑:如果您无法使用 TemplateBinding,而是拥有一个带有相应 .xaml.cs 文件的 .xaml 文件,那么您就拥有了所谓的 用户控件,而不是自定义控件。在那种情况下,
您可以尝试使用如下所示的方法在 CustomControlA 的 XAML 中设置选项卡索引:

TabIndex="{Binding Path=TabIndex, RelativeSource={RelativeSource AncestorType={x:Type my:CustomControlA}}}"

I don't have your custom control to hand, so I created one that contained just a TextBox. After wiring this up to your XAML, I found that the tab order went as follows:

TextBox1, TextBox2, CustomControl1, CustomControl2, TextBox3, TextBox within CustomControl1, TextBox within CustomControl2.

Note that after TextBox2 tabbing moves the focus onto the custom controls themselves, not any child controls they happen to have. The TextBox in my custom control didn't have a TabIndex, so its TabIndex was assumed to be the default value, Int32.MaxValue (see the MSDN documentation for the TabIndex property). Hence they come last in the tab order.

I found that things worked better if I marked the custom controls as IsTabStop="False" (I don't see why I'd want the focus on the custom controls themselves), and set the tab-index of the TextBox in the custom control to that given in the <my:CustomControl /> element by adding the attribute TabIndex="{TemplateBinding TabIndex}" to the TextBox in the custom control. Once I'd done that, the tab order was, as I would expect,

TextBox1, TextBox2, TextBox within CustomControl1, TextBox within CustomControl2, TextBox3.

Of course, my custom control consists only of a single TextBox, so setting a single tab-index fixed things for me. I don't have your code, so I can't say for sure, but it might be enough to set the tab-index of all child controls within your custom controls to that in the <my:CustomControl /> element in the same way.

EDIT: if you can't use a TemplateBinding, and instead have a .xaml file with a corresponding .xaml.cs file, then you have what's called a user control, not a custom control. In that case,
you can try setting the tab index inside the XAML for CustomControlA using something like the following:

TabIndex="{Binding Path=TabIndex, RelativeSource={RelativeSource AncestorType={x:Type my:CustomControlA}}}"
一向肩并 2024-11-06 09:45:23

请发布您的 XAML 代码。同时,需要考虑的一些事项包括:

  • 再次检查 KeyboardNavigation.TabIndex 属性值
  • 您是否在运行时动态添加自定义选项卡?
  • 您是否在运行时以编程方式更改自定义控件或与自定义控件交互?
  • 确保选项卡在 XAML 标记中以正确的顺序列出

Please post your XAML code. In the meantime, some things to consider include:

  • Check the KeyboardNavigation.TabIndex attribute values one more time
  • Are you dynamically adding the custom tabs at runtime?
  • Are you programmatically altering or interacting with the custom controls at runtime?
  • Ensure the tabs are listed in the proper order, in the XAML markup
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文