WPF将TabIndex更改为不遵循外观顺序



我正试图根据视觉外观的顺序设置选项卡索引。这意味着当我特别设置为不聚焦时,出现在窗口顶部的按钮会首先聚焦。

以下是控件的结构;

DocPanel
  |
  |---- DockPanel
  |       |----- Button
  |       |----- Button
  |       |----- Button
  |
  |---- Grid
          |----- Canvas
          |---- TabControl
                    |------ TextBox
                    |------ ComboBox

我想要的选项卡顺序;

  1. 画布
  2. 文本框
  3. 组合框
  4. 3个按钮

目前订单为;

  1. 3个按钮
  2. 文本框
  3. 组合框
  4. 画布

我尝试为外部DockPanel设置KeyboardNavigation.TabNavigation="Local"

然后我将TabNavigation.TabIndexTabIndex设置为我想要的数字,但这不起作用。

如果控件直观地显示在窗口的顶部,是否可以在控件显示在底部之后将选项卡索引更改为焦点?

这是我的XAML:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    FocusManager.FocusedElement="{Binding ElementName=pic}"
    Title="Window1" Height="504" Width="929">
<DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" KeyboardNavigation.TabNavigation="Local">
    <DockPanel DockPanel.Dock="Top" Height="30">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
            <Button Content="Save and Close" KeyboardNavigation.TabIndex="4" TabIndex="4"/>
            <Button Content="Forward" KeyboardNavigation.TabIndex="5" TabIndex="5" />
            <Button Content="Delete" KeyboardNavigation.TabIndex="6" TabIndex="6" />
        </StackPanel>
    </DockPanel>
    <Grid DockPanel.Dock="Bottom">
        <Grid.ColumnDefinitions>
            <ColumnDefinition MinWidth="50"/>
            <ColumnDefinition MinWidth="500"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Border BorderBrush="Aqua" BorderThickness="2" >
            <Canvas x:Name="pic" Grid.Column="0" Grid.Row="0" KeyboardNavigation.TabIndex="1" KeyboardNavigation.IsTabStop="True" Focusable="True"  >
                <Canvas.Background>
                    <ImageBrush ImageSource="bookcover.jpg" Stretch="Fill"/>
                </Canvas.Background>
            </Canvas>
        </Border>
        <TabControl x:Name="tabs" Grid.Column="2" Grid.Row="0">
            <TabItem Header="Fax Details" IsTabStop="False">
                <StackPanel>
                <TextBox  Name="fdCustomerFileNumber" HorizontalAlignment="Left" Height="30" KeyboardNavigation.TabIndex="2" TabIndex="2" />
                <ComboBox TabIndex="3" KeyboardNavigation.TabIndex="3" Width="165"  HorizontalAlignment="Left" Height="22" VerticalAlignment="Center" Name="fdDocType" IsEditable="False" />
                </StackPanel>
            </TabItem>
        </TabControl>
    </Grid>
</DockPanel>

您可以将XAML设置为;

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    FocusManager.FocusedElement="{Binding ElementName=pic}"
    Title="Window1" Height="504" Width="929">
    <DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <DockPanel DockPanel.Dock="Top" Height="30">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                <Button Content="Save and Close" TabIndex="4"/>
                <Button Content="Forward" TabIndex="5" />
                <Button Content="Delete" TabIndex="6" />
            </StackPanel>
        </DockPanel>
        <Grid DockPanel.Dock="Bottom">
            <Grid.ColumnDefinitions>
                <ColumnDefinition MinWidth="50"/>
                <ColumnDefinition MinWidth="500"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Border BorderBrush="Aqua" BorderThickness="2" >
                <Canvas x:Name="pic" Grid.Column="0" Grid.Row="0" Focusable="True"  >
                    <Canvas.Background>
                        <ImageBrush ImageSource="bookcover.jpg" Stretch="Fill"/>
                    </Canvas.Background>
                </Canvas>
            </Border>
            <TabControl x:Name="tabs" Grid.Column="2" Grid.Row="0">
                <TabItem Header="Fax Details" IsTabStop="False">
                    <StackPanel>
                        <TextBox  Name="fdCustomerFileNumber" HorizontalAlignment="Left" Height="30" TabIndex="2" />
                        <ComboBox TabIndex="3" Width="165"  HorizontalAlignment="Left" Height="22" VerticalAlignment="Center" Name="fdDocType" IsEditable="False" />
                    </StackPanel>
                </TabItem>
            </TabControl>
        </Grid>
    </DockPanel>
</Window>

然后在代码后面的New Sub中,只需将焦点设置为Canvas;

pic.Focus;

最新更新