【问题标题】:WPF tab order working wrongWPF选项卡顺序工作错误
【发布时间】:2011-04-05 18:03:36
【问题描述】:

我在 WPF 中有一个观点,我一直在努力使标签顺序正确。我有三个文本框(我们称它们为 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>

【问题讨论】:

    标签: c# wpf xaml tab-ordering


    【解决方案1】:

    我手头没有您的自定义控件,所以我创建了一个只包含一个文本框的控件。将其连接到您的 XAML 后,我发现 Tab 键顺序如下:

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

    请注意,在 TextBox2 制表符将焦点移到 自定义控件本身上之后,而不是它们碰巧拥有的任何子控件。我的自定义控件中的 TextBox 没有 TabIndex,因此它的 TabIndex 被假定为默认值 Int32.MaxValue(请参阅MSDN documentation for the TabIndex property)。因此它们在 Tab 键顺序中排在最后。

    我发现如果我将自定义控件标记为IsTabStop="False"(我不明白为什么我希望将焦点放在自定义控件本身上),并且在通过将属性 TabIndex="{TemplateBinding TabIndex}" 添加到自定义控件的 TextBox 中,将自定义控件添加到 &lt;my:CustomControl /&gt; 元素中给定的控件。一旦我这样做了,标签顺序就像我所期望的那样,

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

    当然,我的自定义控件只包含一个文本框,所以设置一个标签索引为我解决了问题。我没有你的代码,所以我不能肯定地说,但是以相同的方式将自定义控件中所有子控件的选项卡索引设置为 &lt;my:CustomControl /&gt; 元素中的选项卡索引可能就足够了。

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

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

    【讨论】:

    • 我的自定义控件不喜欢 TemplateBinding,因为我没有使用模板。还有其他方法可以继承它,还是我需要将其模板化?这就是问题所在,如果我将正确的标签顺序硬编码到相对于父窗体的子控件控件中,它就可以工作。我现在只需要让它更通用。
    • 我刚刚在使用缺少 TabIndex="{TemplateBinding TabIndex}" 的自定义控件时遇到了类似的问题。非常感谢!
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-09-23
      • 1970-01-01
      • 2021-06-06
      • 2011-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      相关资源
      最近更新 更多