2013-05-08 23 views
6

Tôi có một cửa sổ với XAML sau:Tại sao không phải thứ tự Tab giữa/bên trong một công trình TreeView?

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

Khi tôi chạy chương trình, thứ tự tab là:

 
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 

Các hành vi mong đợi là nó sẽ lặp đi lặp lại từ # 1 đến # 6 trên lặp đi lặp lại hơn nữa, tuy nhiên thay vào đó nó bỏ qua # 4 và # 5 trên bất kỳ lần lặp tiếp theo nào.

Tại sao điều này? Và làm thế nào tôi có thể sửa nó?

Trả lời

3

Ồ, đây là UGLY. Các WPF treeview là mục tiêu yêu thích của tôi cho tính từ này trong rất nhiều khía cạnh, nhưng tôi đã không gặp vấn đề cụ thể này trước đây.

Tôi không nghĩ rằng bạn có thể làm tốt hơn Continue cho KeyboardNavigation.TabNavigation, khả năng duy nhất khác là Local và điều đó cũng không hoạt động. Xem this msdn article. Ngoài ra, các kết hợp như Cycle trên các mục và Continue trên điều khiển không tạo ra kết quả mong muốn cho tôi. Khá rõ ràng, mục đã chọn được lưu giữ trong ảnh tre khi tiêu điểm quay trở lại chế độ xem danh sách và khi treeview nhận tiêu điểm trong lần tiếp theo, nó sẽ chuyển tiêu điểm trực tiếp đến mục đã chọn trước đó (Mục Cây 2), không giống như listview, trong đó bảo tồn lựa chọn nhưng tập trung chính xác điều khiển đầu tiên, sau đó là các mục.

Vì vậy, một hack nhanh chóng và bẩn mà có thể làm việc cho bạn là để loại bỏ các lựa chọn từ treeview khi nó mất tập trung. Thật không may, sự kiện cháy mỗi khi các mục được lựa chọn thay đổi, nhưng các công trình hack tuy nhiên.

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

mã sau:

private void TreeView_LostFocus(object sender, RoutedEventArgs e) 
{ 
    TreeView tv = (TreeView)sender; 
    TreeViewItem item = tv.SelectedItem as TreeViewItem; 
    if (item != null) 
     item.IsSelected = false; 
} 
Các vấn đề liên quan