2014-06-10 59 views
0

我想將我的Node類的對象綁定到樹視圖。將包含不同列表<T>的自定義對象綁定到TreeView

節點類

public class Node 
    { 
     public string Text { get; set; } 
     public List<Node> Child { get; set; } 
     public List<NodeAttribute> Attributes { get; set; } 
     public bool IsLeafNode { get; set; } 
     public bool HasAttributes { get; set; } 
    } 

NodeAttribute

public class NodeAttribute 
    { 
     public string Attribute { get; set; } 
     public string Value { get; set; } 
    } 

我能夠使用HierarchicalDataTemplate而不是屬性來dislplay兒童。

<TreeView Name="tv" ItemsSource="{Binding Child}"> 
      <TreeView.Resources> 
       <HierarchicalDataTemplate ItemsSource="{Binding Child}" DataType="{x:Type tvcc:Node}"> 
        <StackPanel> 
         <TextBlock Text="{Binding Text}"/> 
        </StackPanel> 
       </HierarchicalDataTemplate> 
      </TreeView.Resources> 
     </TreeView> 

我想樣式Child和屬性不同。

回答

1

你需要一個CompositeCollection到兩個孩子和屬性他們

我準備了一個樣品給你

新增的節點類CompositeCollection ChildAndAttributes

public class Node 
{ 
    public Node() 
    { 
     Child = new List<Node>(); 
     Attributes = new List<NodeAttribute>(); 
    } 
    public string Text { get; set; } 
    public List<Node> Child { get; set; } 
    public List<NodeAttribute> Attributes { get; set; } 
    public bool IsLeafNode { get; set; } 
    public bool HasAttributes { get; set; } 

    CompositeCollection comp; 
    public CompositeCollection ChildAndAttributes 
    { 
     get 
     { 
      if (comp == null) 
      { 
       comp = new CompositeCollection(); 
       comp.Add(new CollectionContainer() { Collection = Child }); 
       comp.Add(new CollectionContainer() { Collection = Attributes }); 
      } 
      return comp; 
     } 
    } 
} 

你結合並綁定到HierarchicalDataTemplate的的ItemsSource可以根據需要調整複合材料集合的屬性,只需更改添加到集合的順序

XAML

<TreeView Name="tv"> 
     <TreeView.Resources> 
      <!--created seperate tempates for Node and NodeAttribute--> 
      <DataTemplate DataType="{x:Type tvcc:Node}"> 
       <TextBlock Text="{Binding Text}" 
          Foreground="Navy" /> 
      </DataTemplate> 
      <DataTemplate DataType="{x:Type tvcc:NodeAttribute}"> 
       <TextBlock Text="{Binding Attribute}" 
          Foreground="Crimson" /> 
      </DataTemplate> 
     </TreeView.Resources> 
     <!--moved your original template to Item Template or tree--> 
     <TreeView.ItemTemplate> 
      <!--binded ChildAndAttributes to ItemsSource--> 
      <HierarchicalDataTemplate ItemsSource="{Binding ChildAndAttributes}"> 
       <!--this will pick up the data template for respective element--> 
       <ContentControl Content="{Binding}" /> 
      </HierarchicalDataTemplate> 
     </TreeView.ItemTemplate> 
     <!--below is sample data--> 
     <tvcc:Node Text="hello"> 
      <tvcc:Node.Attributes> 
       <tvcc:NodeAttribute Attribute="attribute child" /> 
      </tvcc:Node.Attributes> 
      <tvcc:Node.Child> 
       <tvcc:Node Text="hello child"> 
        <tvcc:Node.Attributes> 
         <tvcc:NodeAttribute Attribute="attribute child 1" /> 
        </tvcc:Node.Attributes> 
       </tvcc:Node> 
      </tvcc:Node.Child> 
     </tvcc:Node> 
     <tvcc:Node Text="hello2"> 
      <tvcc:Node.Attributes> 
       <tvcc:NodeAttribute Attribute="attribute child 2" /> 
      </tvcc:Node.Attributes> 
      <tvcc:Node.Child> 
       <tvcc:Node Text="hello child 2"> 
        <tvcc:Node.Attributes> 
         <tvcc:NodeAttribute Attribute="attribute child 2" /> 
        </tvcc:Node.Attributes> 
       </tvcc:Node> 
      </tvcc:Node.Child> 
     </tvcc:Node> 
    </TreeView> 

您可以修改數據模板,按您的需求

替代做法

您還可以修改現有的模板,而無需使用CompositCollections實現類似

<HierarchicalDataTemplate ItemsSource="{Binding Child}" 
          DataType="{x:Type tvcc:Node}"> 
    <StackPanel> 
     <TextBlock Text="{Binding Text}" 
        Foreground="Navy" /> 
     <ItemsControl ItemsSource="{Binding Attributes}" Margin="10,0,0,0"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <TextBlock Text="{Binding Attribute}" 
           Foreground="Crimson" /> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </StackPanel> 
</HierarchicalDataTemplate> 

你可以嘗試並選擇最適合你的東西r需要

1

您將不得不使用CompositeCollection來解決您的問題。

這是您的節點轉換成複合收集

public class CompositeNodeConverter : IValueConverter 
    { 

     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      var node = value as Node; 

      CompositeCollection collection = new CompositeCollection(); 
      CollectionContainer container = new CollectionContainer(); 
      container.Collection = node.Child; 
      collection.Add(container); 

      container = new CollectionContainer(); 
      container.Collection = node.Attributes; 

      collection.Add(container); 

      return collection; 

     } 

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

的轉換器,然後你可以在XAML綁定此爲:

<TreeView Name="tv" ItemsSource="{Binding NodeCollection}"> 
     <TreeView.Resources> 
      <controls:CompositeNodeConverter x:Key="CompositeNodeConverter"></controls:CompositeNodeConverter> 

      <HierarchicalDataTemplate ItemsSource="{Binding Converter={StaticResource CompositeNodeConverter}}" DataType="{x:Type controls:Node}"> 
       <StackPanel> 
        <TextBlock Text="{Binding Text}"/> 
       </StackPanel> 
      </HierarchicalDataTemplate> 
      <DataTemplate DataType="{x:Type controls:NodeAttribute}"> 
       <StackPanel> 
        <TextBlock Text="{Binding Value}"/> 
       </StackPanel> 
      </DataTemplate> 
     </TreeView.Resources> 
    </TreeView> 
相關問題