WPF 选项卡键导航 [英] WPF Tab Key Navigation

查看:22
本文介绍了WPF 选项卡键导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个基于 WPF .NET 4.0 C# 的应用程序.我们从 XML 定义(不是 XAML)构建我们的用户界面,但在下面我们使用 WPF 来呈现 UI.也就是说,在运行时,我们基于 XML 定义创建 WPF UI.

We have a WPF .NET 4.0 C# based application. We built our user interface from XML definitions (not XAML) but underneath we use a WPF to present the UI. That is at runtime, we create the WPF UI based on our XML definition.

标签导航有问题.我们为文本和组合框控件设置了 TabStop、TabIndex.
但是标签导航不起作用.如何使标签导航适用于此布局?

We have a problem with tab navigation. We set TabStop, TabIndex, for text and combo box controls.
But tab navigation is not working. How to make tab navigation work for this layout?

推荐答案

WPF 将整个 UI 树视为单个 Tab 范围.它不会像您期望的那样分解成更小的区域.这包括 UserControls 中的控件.

WPF treats the entire UI Tree as a single Tab scope. It isn't broken up into smaller areas such as you would expect. This includes controls inside UserControls.

例如,如果你有

<StackPanel>
    <TextBox Name="TextBox1" />
    <MyUserControl />
    <TextBox Name="TextBox3" />
</StackPanel>

MyUserControl 看起来像

<MyUserControl>
    <TextBox Name="TextBox2"  />
</MyUserControl>

默认选项卡循环为 TextBox1、TextBox2、TextBox3.这是因为没有定义 TabIndex 属性,所以所有控件都以默认的 Tab 键顺序运行,也就是它们添加到 UI 中的顺序.

The default tab cycle would be TextBox1, TextBox2, TextBox3. This is because no TabIndex properties are defined, so all controls run at the default tab order, which is the order in which they're added to the UI.

如果您在控件上设置 TabIndex,如下所示,

If you set the TabIndex on your controls such as below,

<StackPanel>
    <TextBox Name="TextBox1" TabIndex="1" />
    <MyUserControl TabIndex="2" />
    <TextBox Name="TextBox3" TabIndex="3" />
</StackPanel>

您的制表符将更改为 TextBox1、TextBox3、TextBox2.这是因为 TextBox2 没有指定 TabIndex,因此假定为默认值,并且在所有其他指定了 TabIndex 的控件循环通过之后,它会被选项卡化.

Your tabbing would change to TextBox1, TextBox3, TextBox2. This is because TextBox2 doesn't have a TabIndex specified, so the default is assumed and it is tabbed to after all the other controls with a TabIndex specified get cycled through.

我通常解决这个问题的方法是将 UserControl 内控件的 TabIndex 绑定到 UserControl.TabIndex.

The way I usually get around this is to bind the TabIndex of controls inside the UserControl to the UserControl.TabIndex.

例如,将以下绑定添加到 UserControl 将使 Tab 循环再次正确

For example adding the following binding to the UserControl would make the Tab cycle correct again

<MyUserControl>
    <TextBox Name="TextBox2" TabIndex="{Binding Path=TabIndex, RelativeSource={RelativeSource AncestorType={x:Type local:MyUserControl}}}" />
</MyUserControl>

我通常更喜欢在 UserControl 的 Loaded 事件中设置此绑定,而不必记住在 UserControl 内的所有控件上设置此绑定.我相信还有更有效的方法可以做到这一点,但是问题出现的频率还不够高,让我坐下来花时间研究如何正确使用选项卡范围以避免这种解决方法.

I usually prefer to set this binding in the Loaded event of the UserControl instead of having to remember to set this binding on all the controls inside the UserControl. I'm sure there are also more efficient ways of doing this as well, however the problem has not come up often enough for me to sit down and take the time to research how to use tab scopes correctly to avoid this workaround.

这篇关于WPF 选项卡键导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆