2013-02-25 16 views
0

我试图设置Tab顺序以跟随视觉顺序。这意味着当我专门将它设置为不显示时,窗口顶部出现的按钮会首先关注。WPF更改TabIndex以不遵循视觉外观顺序

下面介绍了控件的结构;

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

我想要的Tab顺序;

  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> 

回答

0

您可以设置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; 
+0

谢谢。我做了以下但不工作pic.Focus(); Keyboard.Focus(PIC);另外,我的主要问题是如何在文本框和组合框之后的末尾聚焦按钮。请帮忙! – user19100 2013-02-26 15:25:24