Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF标签顺序工作错误_C#_Wpf_Xaml_Tab Ordering - Fatal编程技术网

C# WPF标签顺序工作错误

C# WPF标签顺序工作错误,c#,wpf,xaml,tab-ordering,C#,Wpf,Xaml,Tab Ordering,我在WPF中有一个观点,我一直在努力使标签顺序正确。我有三个文本框(我们称它们为Text1、Text2和Text3)和两个自定义控件,每个控件上都有几个其他文本框和各种控件(我们称它们为Custom1和Custom2) 布局是这样的,选项卡流应该是Text1、Text2、Custom1、Custom2、Text3。我已在每个控件上设置TabIndex属性以匹配此顺序,并验证所有控件都已设置为IsTabStop 问题是,实际的选项卡流是Text1、Text2、Text3,然后是Custom1、Cu

我在WPF中有一个观点,我一直在努力使标签顺序正确。我有三个文本框(我们称它们为Text1、Text2和Text3)和两个自定义控件,每个控件上都有几个其他文本框和各种控件(我们称它们为Custom1和Custom2)

布局是这样的,选项卡流应该是Text1、Text2、Custom1、Custom2、Text3。我已在每个控件上设置TabIndex属性以匹配此顺序,并验证所有控件都已设置为IsTabStop

问题是,实际的选项卡流是Text1、Text2、Text3,然后是Custom1、Custom2,我不知道为什么。当它转到自定义控件时,它确实会像我所期望的那样正确地跨过其中的每个控件。我只是不明白为什么它会在进入第一个自定义控件之前进入第三个文本框

我已经尝试了我能想到的一切,包括确保所有xaml元素都按制表符顺序排列,但似乎没有任何帮助

我怀疑它在关注任何自定义控件之前,都在检查所有的基本控件,但我没有主意。任何帮助都会很好

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

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

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

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

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

      请注意,在TextBox2选项卡将焦点移动到自定义控件本身,而不是它们碰巧拥有的任何子控件上之后。我的自定义控件中的TextBox没有TabIndex,因此假定其TabIndex为默认值Int32.MaxValue(请参阅)。因此,它们在标签顺序中排在最后

      我发现如果我将自定义控件标记为
      IsTabStop=“False”
      (我不明白为什么我希望将焦点放在自定义控件本身),并通过添加属性
      TabIndex=“{TemplateBinding TabIndex}”,将自定义控件中文本框的选项卡索引设置为
      元素中给定的索引,效果会更好
      到自定义控件中的文本框。一旦我这么做了,账单的顺序就像我预料的那样

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

      当然,我的自定义控件只包含一个文本框,所以设置一个选项卡索引可以解决我的问题。我没有你的代码,所以我不能确定,但是用同样的方法将自定义控件中所有子控件的选项卡索引设置为
      元素中的选项卡索引就足够了

      编辑:如果您不能使用
      模板绑定
      ,而是将.xaml文件与相应的.xaml.cs文件一起使用,那么您就拥有了所谓的用户控件,而不是自定义控件。那么,, 您可以尝试使用以下方法在XAML中为CustomControlA设置选项卡索引:

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

      我的自定义控件不喜欢TemplateBinding,因为我没有使用模板。是否有其他方法继承它,或者我需要将其模板化?但是,如果我将右制表符顺序硬编码到相对于父窗体的子控件中,这就是问题所在。我现在只需要让它更一般化。我只是在使用一个自定义控件时遇到了一个类似的问题,其中缺少TabIndex=“{TemplateBinding TabIndex}”。非常感谢!