WPF Tab键导航 [英] WPF Tab Key Navigation

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

问题描述

我们有一个WPF .NET 4.0 C#应用程序的基础。我们建立了我们的用户界面,从XML定义(不XAML),但下面我们使用WPF来present的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树作为一个单独的标签范围。它不分解成更小的区域,如你所期望的。这里面包括用户控件控制。

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属性的定义,因此,所有控件的默认选项卡顺序,这是他们正在加入到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.

我通常解决这个获得的方式是绑定的TabIndex 控件的用户控件到UserControl.TabIndex里面。

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

例如添加以下绑定到用户控件会使标签循环再正确

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>

我通常preFER设置在该用户的加载情况下,本绑定,而不必记得设置在用户控件内的所有控件此绑定。我敢肯定,也有这样做的,以及更有效的方式,但是问题一直没有拿出往往足以让我坐下来研究如何正确使用标签范围,以避免这种解决办法的时间。

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 Tab键导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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