WPF将TabItem的控件包含在Tab键顺序中

时间:2014-01-15 07:11:04

标签: .net wpf tab-ordering

希望你能给我一个提示我做错了什么。我认为实现我正在尝试的东西会很容易,但我无法解决我的问题。

我想做什么?

我有一个包含几个控件的表单,例如左侧的TextBoxes。在右侧,我有一个带TabItems的TabControl,在这些项目上还有几个例如文本框。如果我打开表单,左侧的第一个TextBox将具有焦点。我以某种方式输入了TabIndex,第一个TabItem上的第一个TextBox(可见)应该在之后获得焦点。但无论我输入什么,在第一个TabItem获得它之前,左侧的所有TextBox都会得到焦点。请在下面找到示例代码。我错了什么?

<Grid Name="grid" >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="200"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <StackPanel Grid.Column="0">
        <TextBox Name="tb1" TabIndex="0" Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
        <TextBox TabIndex="5" Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
        <TextBox TabIndex="1" Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
    </StackPanel>
    <TabControl Grid.Column="1" Height="Auto" Width="Auto" KeyboardNavigation.DirectionalNavigation="Contained">
        <TabItem Header="1">
            <StackPanel>
                <TextBox TabIndex="4" Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
                <TextBox TabIndex="3" Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
                <TextBox TabIndex="6" Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
            <TextBox/>
            </StackPanel>
        </TabItem>
        <TabItem Header="2">
            <StackPanel>
                <TextBox Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
                <TextBox Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
                <TextBox Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
                <TextBox Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
            </StackPanel>
        </TabItem>
    </TabControl>
</Grid>

非常感谢, 托

1 个答案:

答案 0 :(得分:0)

选项卡索引将在每个TabItem中循环,然后它将尝试更进一步(即如果TabIndex被定义为从一个选项卡跳转到另一个选项卡,在您的情况下它不是)。所以你正在做的事情不会起作用。尽管可能很糟糕,但在你的情况下,你必须使用后面的代码将焦点设置为当前TabItem之外的元素,如果你想打破该TabItem上的内部制表循环并跳出它你描述的顺序 。 这里&#39;我为此写了一个快速的样本:

private void TextBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key != Key.Tab) return;

        var textBox = (TextBox)sender;

        switch(textBox.Name)
        {
            case "tb1": 
                tab1.Focus(); 
                tb3.Focus();
                e.Handled = true;
                break;
            case "tb4": 
                tb5.Focus(); 
                e.Handled = true;
                break;
            case "tb5": 
                tab1.Focus(); 
                tb6.Focus(); 
                e.Handled = true;
                break;
        }
    }

 <Grid Name="grid" >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="200"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <StackPanel Grid.Column="0">
        <TextBox Name="tb0" KeyDown="TextBox_KeyDown" TabIndex="0" Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
        <TextBox Name="tb5" KeyDown="TextBox_KeyDown" TabIndex="5" Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
        <TextBox Name="tb1" KeyDown="TextBox_KeyDown" TabIndex="1" Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
    </StackPanel>
    <TabControl Grid.Column="1" Height="Auto" Width="Auto" KeyboardNavigation.DirectionalNavigation="Contained">
        <TabItem x:Name="tab1" Header="1" KeyboardNavigation.DirectionalNavigation="Continue">
            <StackPanel>
                <TextBox Name="tb4" KeyDown="TextBox_KeyDown" TabIndex="4" Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
                <TextBox Name="tb3" KeyDown="TextBox_KeyDown" TabIndex="3" Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
                <TextBox Name="tb6" KeyDown="TextBox_KeyDown" TabIndex="6" Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
                <TextBox/>
            </StackPanel>
        </TabItem>
        <TabItem x:Name="tab2" Header="2">
            <StackPanel>
                <TextBox Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
                <TextBox Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
                <TextBox Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
                <TextBox Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=TabIndex}"/>
            </StackPanel>
        </TabItem>
    </TabControl>
</Grid>