2016-12-02 39 views
0

我想創建動態樹結構,我有紅色的一些教程,但我不明白。我的wpf樹視圖只顯示根元素。我究竟做錯了什麼?WPF TreeView只顯示根元素

這裏是主窗口:

<Window x:Class="test.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:test" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
    <TreeView Name="tv" ItemsSource="{Binding}"> 
     <TreeView.ItemTemplate> 
     <HierarchicalDataTemplate DataType="{x:Type local:Leaf}" 
            ItemsSource="{Binding Childs}">    
      <TextBlock Text="{Binding Path=Name}" /> 
     </HierarchicalDataTemplate> 
     </TreeView.ItemTemplate> 
    </TreeView> 
    </Grid> 
</Window> 

這裏是我的節點類

class Leaf 
{ 
    public string Name { get; set; } 
    public string Address { get; set; } 
    public string Access { get; set; } 
    public string Type { get; set; } 
    public object Value { get; set; } 
    public List<Leaf> Childs; 

    public Leaf() 
    { 
     this.Name = "default"; 
     this.Childs = new List<Leaf>(); 
    } 
} 

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     List<Leaf> mainlist = new List<Leaf>(); 

     Leaf one = new Leaf(); 
     one.Name = "one"; 

     var two = new Leaf(); 
     two.Name = "two"; 

     one.Childs.Add(two); 

     mainlist.Add(one); 

     tv.DataContext = mainlist; 
    } 
} 

回答

0

好吧,這似乎楓葉的那個目錄應該是這樣的:

公開名單童車{得到;設置;}

問題解決了,對不起,爲了休整。