WPF选项卡顺序工作错误 [英] WPF tab order working wrong

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

问题描述

我在WPF中有一个视图,我一直在努力使制表符顺序正确无误.我有三个文本框(分别称为Text1,Text2和Text3)和两个自定义控件,每个控件上都具有其他几个文本框和各种控件(分别称为Custom1和Custom2).

I have a view in WPF that I have been fighting to get the tab order correct on. I have three text boxes (lets call them Text1, Text2, and Text3) and two custom controls, each of which has several other text boxes and assorted controls on them (lets call them Custom1 and Custom2).

布局应使选项卡流应转到Text1,Text2,Custom1,Custom2,Text3.我已经在每个控件上设置了TabIndex属性以匹配此顺序,并验证了它们全部都设置为IsTabStop.

The layout is such that tab flow should go Text1, Text2, Custom1, Custom2, Text3. I have set the TabIndex property on each control to match this ordering, and verified that all of them are set to IsTabStop.

问题是,实际的选项卡流分别为Text1,Text2,Text3,然后依次为Custom1,Custom2,但我不知道原因.当涉及到自定义控件时,它确实可以按我期望的那样正确地逐步执行其中的每个控件.我只是想不通为什么先进入第三个自定义控件才进入第三个文本框.

The problem is, the actual tab flow goes Text1, Text2, Text3, and then Custom1, Custom2, and I cannot figure out why. When it goes to the custom controls, it does properly step over every control in them as I would expect. I just can't figure out why it goes to the third text box before it goes to the first custom control.

我已经尝试了所有我能想到的一切,包括确保所有xaml元素都按Tab键顺序排列,但似乎无济于事.

I have tried everything I can think of, including making sure all of the xaml elements are arranged in tab order, but nothing seems to help.

我怀疑在重点关注任何自定义控件之前,它已经遍历了所有基本控件,但是我没有主意.任何帮助都会很棒.

I suspect it is going over all of its basic controls before giving focus to any custom controls, but I am out of ideas. Any help would be great.

这是我的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>

推荐答案

我没有要处理的自定义控件,因此我创建了一个仅包含TextBox的控件.将其连接到您的XAML之后,我发现选项卡顺序如下:

I don't have your custom control to hand, so I created one that contained just a TextBox. After wiring this up to your XAML, I found that the tab order went as follows:

TextBox1,TextBox2,CustomControl1,CustomControl2,TextBox3,CustomControl1中的TextBox,CustomControl2中的TextBox.

TextBox1, TextBox2, CustomControl1, CustomControl2, TextBox3, TextBox within CustomControl1, TextBox within CustomControl2.

请注意,在TextBox2选项卡上将焦点移到自定义控件本身上之后,它们碰巧没有任何子控件.我的自定义控件中的TextBox没有TabIndex,因此假定其TabIndex为默认值Int32.MaxValue(请参见

Note that after TextBox2 tabbing moves the focus onto the custom controls themselves, not any child controls they happen to have. The TextBox in my custom control didn't have a TabIndex, so its TabIndex was assumed to be the default value, Int32.MaxValue (see the MSDN documentation for the TabIndex property). Hence they come last in the tab order.

我发现,如果将自定义控件标记为 IsTabStop ="False" (我不明白为什么要集中精力于自定义控件本身),效果会更好.通过添加属性 TabIndex ="{TemplateBinding TabIndex}" < my:CustomControl/> 元素中给定的标签索引>转到自定义控件中的TextBox.完成此操作后,按我期望的那样,按Tab键排序

I found that things worked better if I marked the custom controls as IsTabStop="False" (I don't see why I'd want the focus on the custom controls themselves), and set the tab-index of the TextBox in the custom control to that given in the <my:CustomControl /> element by adding the attribute TabIndex="{TemplateBinding TabIndex}" to the TextBox in the custom control. Once I'd done that, the tab order was, as I would expect,

TextBox1,TextBox2,CustomControl1中的TextBox,CustomControl2中的TextBox,TextBox3.

TextBox1, TextBox2, TextBox within CustomControl1, TextBox within CustomControl2, TextBox3.

当然,我的自定义控件仅包含单个TextBox,因此为我设置单个tab-index可以解决问题.我没有您的代码,所以不能肯定地说,但这足以将您的自定义控件中所有子控件的tab-index设置为< my:CustomControl/> 元素.

Of course, my custom control consists only of a single TextBox, so setting a single tab-index fixed things for me. I don't have your code, so I can't say for sure, but it might be enough to set the tab-index of all child controls within your custom controls to that in the <my:CustomControl /> element in the same way.

编辑:如果您不能使用 TemplateBinding ,而是拥有一个.xaml文件和一个相应的.xaml.cs文件,那么您就拥有了一个用户控件,而不是自定义控件.在这种情况下,您可以尝试使用以下方法在XAML中为CustomControlA设置标签索引:

EDIT: if you can't use a TemplateBinding, and instead have a .xaml file with a corresponding .xaml.cs file, then you have what's called a user control, not a custom control. In that case, you can try setting the tab index inside the XAML for CustomControlA using something like the following:

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

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

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