复合控件中的 WPF TabIndex

发布于 2024-08-13 20:54:58 字数 1788 浏览 7 评论 0原文

我有一个简单的窗口,其中嵌入了一个简单的复合控件。

(主窗口)

<Window x:Class="TabOrder.Window1"
xmlns:local="clr-namespace:TabOrder"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
    <Label HorizontalAlignment="Left" VerticalAlignment="Top">First</Label>
    <TextBox TabIndex="0" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,0,0,0"/>

    <Label HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0">Second</Label>
    <TextBox TabIndex="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,30,0,0"/>

    <local:MyControl Margin="0,60,0,0" VerticalAlignment="Top" HorizontalAlignment="Stretch" TabIndex="2"/>
</Grid>

(复合控制)

<UserControl x:Class="TabOrder.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
    <Label HorizontalAlignment="Left" VerticalAlignment="Top">Third</Label>
    <TextBox HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,0,0,0"/>

    <Label HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0">Fourth</Label>
    <TextBox HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,30,0,0"/>
</Grid>

正如我的表单所预期的那样,我得到 4 个文本框...

  • 第一、
  • 第二、
  • 第三、
  • 第四,

但是当“第一”获得焦点并且我点击选项卡时,焦点将切换到“第三”。 WPF 似乎将选项卡列表视为单个平面列表,而不是视为树,其中 MyControl 为 TabIndex 3,文本框“Third”是其中的第一个选项卡式控件。

这是 WPF 中的错误还是有其他方法可以做到这一点?复合控件可在许多窗口中使用,甚至可以在单个窗口上多次使用。

I have a simple window with a simple composite control embedded within it.

(Main Window)

<Window x:Class="TabOrder.Window1"
xmlns:local="clr-namespace:TabOrder"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
    <Label HorizontalAlignment="Left" VerticalAlignment="Top">First</Label>
    <TextBox TabIndex="0" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,0,0,0"/>

    <Label HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0">Second</Label>
    <TextBox TabIndex="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,30,0,0"/>

    <local:MyControl Margin="0,60,0,0" VerticalAlignment="Top" HorizontalAlignment="Stretch" TabIndex="2"/>
</Grid>

(Composite control)

<UserControl x:Class="TabOrder.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
    <Label HorizontalAlignment="Left" VerticalAlignment="Top">Third</Label>
    <TextBox HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,0,0,0"/>

    <Label HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0">Fourth</Label>
    <TextBox HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="80,30,0,0"/>
</Grid>

As expected on my form I get 4 text boxes...

  • First
  • Second
  • Third
  • Fourth

But when "First" has focus and I hit tab the focus is switched to "Third". WPF seems to be seeing the tab list as a single flat list rather than as a tree where MyControl is TabIndex 3 and the text box "Third" the first tabbed control within it.

Is this a bug in WPF or is there another way of doing this? The composite control is used in many windows, it could even be used more than once on a single window.

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

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

发布评论

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

评论(1

终陌 2024-08-20 20:54:58

我知道这个响应已经很晚了...但是您尝试过吗:

<UserControl ... KeyboardNavigation.TabNavigation="Local">

这样做将确保一旦您的 UserControl 获得焦点,您将仅通过 UserControl 内的 TabStop 进行导航(而不用担心整个应用程序中存在冲突的 TabIndex 值)。循环遍历 UserControl 的 TabStop 后,TabNavigation 将恢复到其外部的 TabStop。

http://msdn.microsoft.com/en-us /library/system.windows.input.keyboardnavigationmode.aspx

I know this response is quite late... but have you tried:

<UserControl ... KeyboardNavigation.TabNavigation="Local">

Doing so will ensure once your UserControl has recieved focus, you will navigate only through TabStop within your UserControl (instead of worring about conflicting TabIndex values throughout your app). After looping through the TabStops of your UserControl, TabNavigation will resume to the TabStop outside of it.

http://msdn.microsoft.com/en-us/library/system.windows.input.keyboardnavigationmode.aspx

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