C# 从UserControls使用时在wpf中设置选项卡顺序?

C# 从UserControls使用时在wpf中设置选项卡顺序?,c#,wpf,user-controls,tab-ordering,C#,Wpf,User Controls,Tab Ordering,我有这样一个用户控件: <UserControl x:Class="MySample.customtextbox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.o

我有这样一个用户控件:

<UserControl x:Class="MySample.customtextbox"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="20" d:DesignWidth="300">
<Grid>
    <TextBox x:Name="Ytextbox"  Background="Yellow"/>
</Grid>

我在窗口中使用此控件并设置选项卡顺序…但是当我的窗口加载时,选项卡顺序无法正常工作!!! 我的窗口代码:

<Window xmlns:my="clr-namespace:MySample"  x:Class="MySample.window"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="window" Height="300" Width="600">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>

    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>

   <my:customtextbox Grid.Column="1" KeyboardNavigation.TabIndex="0" InfoText="{Binding msg}" Height="20"/>
    <TextBox Grid.Column="3" KeyboardNavigation.TabIndex="1" Text="{Binding msg}" Height="20" Background="Gold"></TextBox>
    <my:customtextbox Grid.Row="1" Grid.Column="1" KeyboardNavigation.TabIndex="2" InfoText="{Binding msg}" Height="20"/>
    <TextBox Grid.Column="3"  Grid.Row="1"  Text="{Binding msg}" Height="20" KeyboardNavigation.TabIndex="3" Background="Gold"></TextBox>

</Grid>


默认情况下,WPF在同一选项卡级别读取用户控件内外的所有控件。由于UserControl中的控件没有指定TabIndex,因此它们会在第一个制表周期后被制表

我通常使用的解决方法是在我的
UserControl
上设置
IsTabStop=“False”
(以防止用户控件本身上的选项卡),然后在
UserControl
内部使用
TemplateBinding
将内部控件
TabIndex
绑定到UserControl的
TabIndex

<TextBox x:Name="Ytextbox"  Background="Yellow"
         TabIndex="{Binding Path=TabIndex, 
         RelativeSource={RelativeSource AncestorType={x:Type local:customtextbox}}}"/>



或者,当自定义用户控件具有更多控件时,您可以在
上使用
键盘导航.TabNavigation=“Local”
,这些控件具有自己的特定选项卡顺序,仅在usercontrol@psulek当前位置我建议你回答这个问题,这对我的处境很有帮助,也许其他人会觉得有用(用户控制中的两个用户控制,加上一些常规的小部件)。@psulek,我在互联网上搜索解决方案已经好几个星期了……答案就在一条评论上。老兄,你救了我……非常感谢。。。。
<my:customtextbox IsTabStop="False" KeyboardNavigation.TabIndex="0" 
                  Grid.Column="1" InfoText="{Binding msg}" Height="20"/>