2013-02-25 78 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