用户控件上的焦点和 TabIndex

发布于 2024-12-13 08:52:39 字数 1707 浏览 1 评论 0原文

我有一个奇怪的行为: 我有一个包含文本框和(简单)用户控件(文本框和按钮)的主窗口,但出于调试目的,我将其剥离为仅一个文本框。

当我在不设置 TabIndex 属性的情况下使用文本框和用户控件时,光标会按正确的顺序(按照控件添加到窗口的顺序)

逐步浏览控件。无效顺序(首先是所有用户控件,然后是所有文本框),当 TabIndex 设置为与添加控件的顺序相对应的值时也是如此

这是我的用户控件

<UserControl x:Class="SmallControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             >
        <TextBox x:Name="txTEXT" Text="{Binding Text}" />
</UserControl>

下面的 Mainwindow xaml 导致顺序000000,111111,222222,333333,没关系

    <GroupBox Header="Small,Textbox,Small,TextBox without TabIndex">
        <UniformGrid Columns="4">
            <local:SmallControl Text="000000" />
            <TextBox Text="111111" />
            <local:SmallControl Text="222222" />
            <TextBox Text="333333" />
        </UniformGrid>
    </GroupBox>

下面的Mainwindow xaml导致顺序000000,222222,111111,333333,没关系有

    <GroupBox Header="Small,Textbox,Small,TextBox with TabIndex">
        <UniformGrid Columns="4">
            <local:SmallControl TabIndex="0" Text="000000" />
            <TextBox TabIndex="1" Text="111111" />
            <local:SmallControl TabIndex="2" Text="222222" />
            <TextBox TabIndex="3" Text="333333" />
        </UniformGrid>
    </GroupBox>

没有一种方法可以使用TabIndex而不被迫在XAML中以“正确”的顺序添加控件?

I have a strange behaviour:
I have a MainWindow containing textboxes and (simple) usercontrols (textbox and button), but I stripped this to only a textbox for debug purposes.

When I use textboxes and usercontrols WITHOUT setting a TabIndex property the cursor steps through the controls in right order (in the order the controls are added to the window)

When I use textboxes and usercontrols WITH setting a TabIndex property the cursor steps through the controls in invalid order (first all usercontrols, then all textboxes), this is also true when the TabIndex is set to value corresponding to the order in which the control was added

Here is my usercontrol

<UserControl x:Class="SmallControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             >
        <TextBox x:Name="txTEXT" Text="{Binding Text}" />
</UserControl>

The following Mainwindow xaml leads to order 000000,111111,222222,333333 ,thats ok

    <GroupBox Header="Small,Textbox,Small,TextBox without TabIndex">
        <UniformGrid Columns="4">
            <local:SmallControl Text="000000" />
            <TextBox Text="111111" />
            <local:SmallControl Text="222222" />
            <TextBox Text="333333" />
        </UniformGrid>
    </GroupBox>

The following Mainwindow xaml leads to order 000000,222222,111111,333333, thats NOT ok

    <GroupBox Header="Small,Textbox,Small,TextBox with TabIndex">
        <UniformGrid Columns="4">
            <local:SmallControl TabIndex="0" Text="000000" />
            <TextBox TabIndex="1" Text="111111" />
            <local:SmallControl TabIndex="2" Text="222222" />
            <TextBox TabIndex="3" Text="333333" />
        </UniformGrid>
    </GroupBox>

Is there a way to use TabIndex without beeing forced to add controls in the "right" order in XAML?

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

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

发布评论

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

评论(1

本宫微胖 2024-12-20 08:52:39

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

要更改此行为,我通常在 UserControl 定义上设置 IsTabStop="False" ,然后将内部控件 TabIndex 绑定到 UserControl 的 TabIndex

UserControl XAML

<TextBox x:Name="txTEXT" Text="{Binding Text}" 
         TabIndex="{Binding Path=TabIndex, RelativeSource={RelativeSource 
             AncestorType={x:Type local:SearchView}}}"/>

用法 XAML

<GroupBox Header="Small,Textbox,Small,TextBox with TabIndex">
    <UniformGrid Columns="4">
        <local:SmallControl TabIndex="0" Text="000000" IsTabStop="False" />
        <TextBox TabIndex="1" Text="111111" />
        <local:SmallControl TabIndex="2" Text="222222" IsTabStop="False" />
        <TextBox TabIndex="3" Text="333333" />
    </UniformGrid>
</GroupBox>

您也可以通过以下方式正确使用 Tab 键设置 KeyboardNavigation.TabNavigation 将 UserControl 上的属性附加到 本地。我似乎记得有这方面的问题,但老实说我不记得细节,所以它可能有用。

<UserControl x:Class="SmallControl" ...
             KeyboardNavigation.TabNavigation="Local"  />

By default, WPF reads all the controls, both 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.

To change this behavior I usually set IsTabStop="False" on my UserControl definition, then I bind the inner controls TabIndex to the UserControl's TabIndex

UserControl XAML

<TextBox x:Name="txTEXT" Text="{Binding Text}" 
         TabIndex="{Binding Path=TabIndex, RelativeSource={RelativeSource 
             AncestorType={x:Type local:SearchView}}}"/>

Usage XAML

<GroupBox Header="Small,Textbox,Small,TextBox with TabIndex">
    <UniformGrid Columns="4">
        <local:SmallControl TabIndex="0" Text="000000" IsTabStop="False" />
        <TextBox TabIndex="1" Text="111111" />
        <local:SmallControl TabIndex="2" Text="222222" IsTabStop="False" />
        <TextBox TabIndex="3" Text="333333" />
    </UniformGrid>
</GroupBox>

You might also be able to get it tabbing correctly by setting the KeyboardNavigation.TabNavigation attached property on your UserControl to Local. I seem to recall having issues with this, but I honestly can't remember the details, so it might work.

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