2011-01-20 89 views
2

如果您的數據在集合中組織,您可以使用HierarchicalDataTemplates填充TreeView。但是我有一些數據,保存在一些衆所周知的類中,需要在TreeView中顯示。下面是一個代碼示例:TreeView遞歸沒有集合

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Collections.ObjectModel; 

namespace Test 
{ 
    public class GroupWithList 
    { 
     public string Name { get; set; } 

     public ObservableCollection<string> Items { get; set; } 

     public GroupWithList(string name) 
     { 
      this.Name = name; 
      this.Items = new ObservableCollection<string>(); 
     } 
    } 

    public class GroupWith2Children 
    { 
     public string Name { get; set; } 

     public ObservableCollection<GroupWithList> Groups { get; set; } 

     public GroupWithList First 
     { 
      get 
      { 
       return this.Groups[0]; 
      } 
     } 

     public GroupWithList Second 
     { 
      get 
      { 
       return this.Groups[1]; 
      } 
     } 

     public GroupWith2Children() 
     { 
      this.Name = "GroupWith2Children"; 
      this.Groups = new ObservableCollection<GroupWithList>(); 

      GroupWithList g; 

      g = new GroupWithList("Letters"); 
      g.Items.Add("u"); 
      g.Items.Add("a"); 
      g.Items.Add("t"); 
      this.Groups.Add(g); 

      g = new GroupWithList("Numbers"); 
      g.Items.Add("12"); 
      g.Items.Add("153"); 
      this.Groups.Add(g); 
     } 
    } 
} 

public partial class MainWindow : Window 
{ 
    public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(ObservableCollection<GroupWith2Children>), typeof(MainWindow), new FrameworkPropertyMetadata(new ObservableCollection<GroupWith2Children>())); 

    public ObservableCollection<GroupWith2Children> Items 
    { 
     get 
     { 
      return (ObservableCollection<GroupWith2Children>)this.GetValue(ItemsProperty); 
     } 

     set 
     { 
      this.SetValue(ItemsProperty, value); 
     } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 

     this.Items.Add(new GroupWith2Children()); 
     this.Items.Add(new GroupWith2Children()); 
    } 
} 

這裏是XAML代碼:

<Window x:Class="Test.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:sys="clr-namespace:System;assembly=mscorlib" 
     xmlns:test="clr-namespace:Test" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Grid.Resources> 

      <DataTemplate DataType="{x:Type test:GroupWith2Children}"> 
       <TreeViewItem Header="{Binding Mode=OneWay, Path=Name}"> 
        <TreeViewItem Header="First" ItemsSource="{Binding Mode=OneWay, Path=First.Items}"/> 
        <TreeViewItem Header="Second" ItemsSource="{Binding Mode=OneWay, Path=Second.Items}"/> 
       </TreeViewItem> 
      </DataTemplate> 

      <HierarchicalDataTemplate DataType="{x:Type test:GroupWithList}" ItemsSource="{Binding Path=Items}"> 
       <TextBlock Text="GroupWithList"/> 
      </HierarchicalDataTemplate> 

      <DataTemplate DataType="{x:Type sys:String}"> 
       <TextBlock Text="{Binding}"/> 
      </DataTemplate> 
     </Grid.Resources> 

     <TreeView> 
      <TreeViewItem Header="Root" ItemsSource="{Binding Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=Items}"/> 
     </TreeView> 
    </Grid> 
</Window> 

的例子能正常工作,但你不能選擇這是在DataTemplate中生成GroupWith2Children的項目。看起來TreeView將整個模板視爲一個項目。您只能選擇根節點和整個子樹「GroupWith2Children」。

用集合取代GroupWith2Children對我來說不是一種選擇。任何想法,我做錯了什麼?

回答

1

您不必用集合取代GroupWith2Children;你可以實現一個枚舉屬性。如果你不能在所有觸摸GroupWith2Children類中,實現一個包裝類:

public class GroupWrapper() 
{ 
    public GroupWrapper(GroupWith2Children group) 
    { 
     Group = group; 
    } 

    public GroupWith2Children Group { get; private set; ] 

    public IEnumerable<GroupWithList> Groups 
    { 
     get 
     { 
     yield return Group.First; 
     yield return Group.Second; 
     } 
    } 
} 
+0

的作品,但我必須使用的IEnumerable (其具有對列表和名稱的引用)來顯示硬編碼名稱(「Group1」和「Group2」)。謝謝 – Paul 2011-01-21 11:03:35