我只是想找到一种方法来控制
TreeView
节点通过它们绑定到的对象。对象具有
IsExpanded
属性,我想用它来显示
树视图
节点本身基于该属性展开或折叠。
以下是我的代码:
C:
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
this.DataContext = new List() { Base.GetParent("Parent 1"), Base.GetParent("Parent 2") };
}
}
public class Base
{
public string Name { get; set; }
public bool IsExpanded { get; set; }
public static Parent GetParent(string name)
{
Parent p = new Parent() { Name = name };
p.Children.Add(new Child() { Name = "Child 1", GrandChildren = new ObservableCollection() { new GrandChild() { Name = "Grandchild 1" } } });
p.Children.Add(new Child() { Name = "Child 2", GrandChildren = new ObservableCollection() { new GrandChild() { Name = "Grandchild 1" } } });
p.Children.Add(new Child() { Name = "Child 3", GrandChildren = new ObservableCollection() { new GrandChild() { Name = "Grandchild 1" } } });
return p;
}
}
public class Parent : Base
{
public ObservableCollection Children { get; set; }
public Parent()
{
this.Children = new ObservableCollection();
}
}
public class Child : Base
{
public ObservableCollection GrandChildren { get; set; }
public Child()
{
this.GrandChildren = new ObservableCollection();
}
}
public class GrandChild : Base
{
}
XAML:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:HeterogeneousTree"
Title="Window2" Height="300" Width="300">