cap*_*aig 4 c# wpf xaml tab-ordering
我在WPF中有一个观点,我一直在努力让Tab键顺序正确.我有三个文本框(让我们称之为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>
Run Code Online (Sandbox Code Playgroud)
Luk*_*ard 10
我没有你的自定义控件,所以我创建了一个只包含TextBox的控件.将此连接到您的XAML后,我发现Tab键顺序如下:
CustomControl1中的TextBox1,TextBox2,CustomControl1,CustomControl2,TextBox3,TextBox,CustomControl2中的TextBox.
请注意,在TextBox2选项卡后,焦点会将焦点移动到自定义控件本身,而不是它们恰好具有的任何子控件.我的自定义控件中的TextBox没有TabIndex,因此它的TabIndex被假定为默认值Int32.MaxValue(请参阅TabIndex属性的MSDN文档).因此,它们以Tab键顺序排在最后.
我发现如果我将自定义控件标记为IsTabStop="False"
(我不明白为什么我希望将焦点放在自定义控件本身上),并且将自定义控件中TextBox的tab-index设置为<my:CustomControl />
通过将属性添加TabIndex="{TemplateBinding TabIndex}"
到自定义控件中的TextBox来实现该元素.一旦我这样做了,正如我所料,标签顺序是
CustomControl1中的TextBox1,TextBox2,TextBox,CustomControl2中的TextBox,TextBox3.
当然,我的自定义控件只包含一个TextBox,因此为我设置一个tab-index固定的东西.我没有你的代码,所以我不能肯定地说,但是可能足以将自定义控件中所有子控件的tab-index设置为<my:CustomControl />
元素中的相同方式.
编辑:如果您不能使用a TemplateBinding
,而是使用.xaml文件与相应的.xaml.cs文件,那么您将拥有所谓的用户控件,而不是自定义控件.在这种情况下,您可以尝试使用以下内容在XAML for CustomControlA中设置选项卡索引:
TabIndex="{Binding Path=TabIndex, RelativeSource={RelativeSource AncestorType={x:Type my:CustomControlA}}}"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9859 次 |
最近记录: |