2013-05-08 78 views
6

我有下面的XAML的窗口:爲什麼TreeView之間/ TabView內的順序不起作用?

<Window x:Class="TestDemoApp.TreeViewWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="TreeViewWindow" Height="300" Width="300"> 
    <Window.Resources> 
     <Style TargetType="Control" x:Key="FocusedStyle"> 
      <Setter Property="Control.Template"> 
       <Setter.Value> 
        <ControlTemplate> 
         <Rectangle StrokeThickness="1" 
           Stroke="Red" 
           StrokeDashArray="1 2 3 4" 
           SnapsToDevicePixels="true"/> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

     <Style TargetType="TreeViewItem"> 
      <Setter Property="IsTabStop" Value="True"/> 
      <Setter Property="Focusable" Value="True"/> 
      <Setter Property="FocusVisualStyle" Value="{StaticResource FocusedStyle}"/> 
      <Setter Property="KeyboardNavigation.TabNavigation" Value="Continue"/> 
     </Style> 
     <Style TargetType="ListViewItem"> 
      <Setter Property="IsTabStop" Value="True"/> 
      <Setter Property="Focusable" Value="True"/> 
      <Setter Property="FocusVisualStyle" Value="{StaticResource FocusedStyle}"/> 
      <Setter Property="KeyboardNavigation.TabNavigation" Value="Continue"/> 
     </Style> 
    </Window.Resources> 

    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 

     <ListView TabIndex="1" BorderThickness="5" Focusable="True" IsTabStop="True" KeyboardNavigation.TabNavigation="Continue" FocusVisualStyle="{StaticResource FocusedStyle}"> 
      <ListViewItem TabIndex="2" Content="List Item 1"/> 
      <ListViewItem TabIndex="3" Content="List Item 2"/> 
     </ListView> 

     <TreeView TabIndex="6" BorderThickness="5" Focusable="True" IsTabStop="True" KeyboardNavigation.TabNavigation="Continue" Grid.Row="1" FocusVisualStyle="{StaticResource FocusedStyle}"> 
      <TreeView.Items> 
       <TreeViewItem TabIndex="7" Header="Tree Item 1"> 
        <TreeViewItem Header="Tree Item 11"></TreeViewItem> 
        <TreeViewItem Header="Tree Item 12"/> 
       </TreeViewItem> 
       <TreeViewItem Header="Tree Item 2"> 
       </TreeViewItem> 
      </TreeView.Items> 
     </TreeView> 
    </Grid> 
</Window> 

當運行該程序時,標籤的順序是:

 
1. List View 
2. List Item 1 
3. List Item 2 
4. Tree View 
5. Tree Item 1 
6. Tree Item 2 

7. List View (#1) 
8. List Item 1 (#2) 
9. List Item 2 (#3) 
10. Tree Item 2 (6#) 

11+ Repeat #7 - #10 

預期的行爲是,它會從#1時重複至#6進一步迭代,然而相反,它會在隨後的迭代中跳過#4和#5。

這是爲什麼?我該如何解決它?

回答

3

哇,這是醜陋的。在許多方面,WPF treeview是我最喜歡的這個形容詞的目標,但是我之前沒有遇到過這個特殊問題。

我不認爲你可以做得比Continue更好的KeyboardNavigation.TabNavigation,唯一的其他可能性是Local,這也行不通。見this msdn article。另外,組合上的項目Cycle和控件上的Continue對我來說沒有產生期望的結果。

很明顯,選中的項目在焦點返回到列表視圖時保存在樹視圖中,並且下次樹視圖接收焦點時,它將焦點直接傳遞到先前選擇的項目(樹項目2),不像listview,它保留了選擇,但是正確地將控件首先聚焦,然後是項目。

因此,可能適用於您的快速而骯髒的黑客攻擊是在失去焦點時從樹視圖中刪除選擇。不幸的是,每當選定的項目發生變化時,事件就會觸發,但黑客仍然有效。

<TreeView (...) LostFocus="TreeView_LostFocus"> 

後面的代碼:

private void TreeView_LostFocus(object sender, RoutedEventArgs e) 
{ 
    TreeView tv = (TreeView)sender; 
    TreeViewItem item = tv.SelectedItem as TreeViewItem; 
    if (item != null) 
     item.IsSelected = false; 
}