1
如何在TreeView中顯示「根」?在TreeView中顯示「根」節點
我是否需要爲根創建一個特殊的HierarchicalDataTemplate?
這是我的代碼:
<TreeView Margin="0,0,15,0" Name="treeView1" ItemsSource="{Binding Path=Children}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Node}"
ItemsSource="{Binding Path=Children}">
<Border BorderBrush="Black" BorderThickness="1">
<StackPanel>
<TextBlock Margin="10,0,0,0" Text="{Binding Data}"/>
</StackPanel>
</Border>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
Node node0 = new Node("Root");
Node node1 = new Node("Node1-1");
Node node12 = new Node("Node1-2");
Node node21 = new Node("Node2-1");
Node node22 = new Node("Node2-2");
Node node31 = new Node("Node3-1");
Node node9 = new Node("Node9");
node12.Children.Add(node21);
node12.Children.Add(node22);
node22.Children.Add(node31);
node1.Children.Add(node9);
node0.Children.Add(node1);
node0.Children.Add(node12);
treeView1.DataContext = node0;
public class Node
{
List<Node> children;
public List<Node> Children
{
get { return children; }
set { children = value; }
}
string data;
public string Data
{
get { return data; }
set { data = value; }
}
public Node()
{
children = new List<Node>();
}
public Node(string data)
: this()
{
this.data = data;
}
}
我不明白,只有一個在其項目指定的ItemsSource? – anderi 2011-06-10 15:11:13
類似於:'treeView.ItemsSource = new List(){rootNode};' –
2011-06-10 15:12:23