1

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

我想做什么?

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

<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>

非常感谢,托斯滕

4

1 回答 1

0

选项卡索引将在每个 TabItem 内循环,然后它会尝试更进一步(也就是说,如果 TabIndex 被定义为从一个选项卡跳转到另一个选项卡,在您的情况下不是)。所以你在做什么是行不通的。TabItem尽管它可能很糟糕,但在您的情况下,如果您想打破该 TabItem 上的内部选项卡循环并按照您描述的顺序跳出它,您将不得不使用后面的代码将焦点设置到当前之外的元素。在这里'我为此写了一个快速示例:

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>
于 2014-01-15T15:56:20.470 回答