我已经解决了,我想。让我知道是否有比这更明智的方法。
这篇博文很有帮助,但不幸的是代码中充满了拼写错误:
David Sackstein 的 - HierarchicalDataTemplate 和 TreeView
关键是绑定到 TreeView 的每个类都派生自一个基类。这允许您通过将不同类型绑定到其基类的集合来将不同类型放入 TreeView。然后在 XAML 中,您可以为每个 DataType 创建一个 HierarchicalDataTemplate,一切正常。
<Window x:Class="HierarchicalDataTemplateAndTreeView.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:HierarchicalDataTemplateAndTreeView"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Task}" ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal" Margin="3">
<TextBlock Text="{Binding Path=Name}" FontSize="16" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:Person}" ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" Foreground="Purple" FontSize="14" FontWeight="Bold" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:ItemCollection}" ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" Foreground="Blue" FontSize="12" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:Item}" ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" Foreground="Blue" FontSize="11" FontStyle="Italic" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:DayCollection}" ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" Foreground="RosyBrown" FontSize="12" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:Day}" ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" Foreground="RosyBrown" FontSize="12" FontStyle="Italic" />
</StackPanel>
</HierarchicalDataTemplate>
</Window.Resources>
<StackPanel>
<TreeView Name="treeView"/>
</StackPanel>
</Window>
using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
namespace HierarchicalDataTemplateAndTreeView
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
treeView.ItemsSource = GetData();
}
public List<Composite> GetData()
{
List<Composite> list = new List<Composite>()
{
new Task
{
Name = "Laundry", Children = new List<Composite>()
{
new Person { Name = "John" },
new ItemCollection
{
Name = "Items", Children = new List<Composite>()
{
new Item { Name = "Clothes" },
new Item { Name = "Washing Powder" }
}
},
new DayCollection
{
Name = "Days", Children = new List<Composite>()
{
new Day { Name = "Sunday" },
new Day { Name = "Thursday" }
}
}
}
},
new Task
{
Name = "Shopping", Children = new List<Composite>()
{
new Person { Name = "Millie" },
new ItemCollection
{
Name = "Items", Children = new List<Composite>()
{
new Item { Name = "Money" },
new Item { Name = "List" },
new Item { Name = "Bags" }
}
},
new DayCollection
{
Name = "Days", Children = new List<Composite>()
{
new Day { Name = "Saturday" }
}
}
}
}
};
return list;
}
}
}
using System.Collections.Generic;
namespace HierarchicalDataTemplateAndTreeView
{
public class Composite
{
public string Name { get; set; }
public List<Composite> Children { get; set; }
}
public class Task: Composite
{
}
public class Person : Composite
{
}
public class ItemCollection : Composite
{
}
public class Item : Composite
{
}
public class DayCollection : Composite
{
}
public class Day : Composite
{
}
}