我想创建动态树结构,我有一些红色的教程,但我不能正确。我的wpf treeview只显示了根元素。我做错了什么?
这是MainWindow:
<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 :(得分:0)
好的,看起来Leaf of Leafs应该是这样的:
public List Childs {get;设置;}
问题解决了,抱歉对不起。