WPF Treeview Binding 的情况有点复杂。我花了过去 2 天的时间尝试谷歌它,这是我想出的关闭,但它并没有解决问题。
情况如下:
我有一个看起来像这样的对象:
public class Category
{
public string Name { get; set; }
public List<Category> Categories { get; set; }
public List<Product> Products { get; set; }
}
public class Product
{
public string Name { get; set;
}
每个类别可以有一个对象列表和子类别。我有这样做的理由,这对我和我正在编写的应用程序来说完全有意义。
实际的对象构造可能看起来像这样:
Category - Pharmacy
|-Product - Aspirin
|-Product - Tylenol
|-Category - Tooth Paste
| |-Product - Crest
| |-Product - Colgate
|-Category - Paper Products
|-Category - Toilet Paper
| |-Product - NoName
| |-Product - Charmin
|-Category - Facial Tissue
|-Product - Kleenex
Category - Household
|-Product - Pinesol Cleaner
|-Product - Garbage Bags
现在,我正在尝试将这种关系数据绑定到树视图。我希望 TreeView 看起来与上述对象构造几乎相同。
到目前为止,我的 XAML Treeview 看起来像这样:
<TreeView x:Name="CategoryList" Margin="8" Grid.Row="2" Grid.RowSpan="2" ItemsSource="{Binding Path=Categories}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type src:Category}" ItemsSource="{Binding Products}">
<StackPanel>
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type src:Product}">
<StackPanel>
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
这适用于主要类别列表及其每个子产品。但它并没有更深入地显示每个类别下的子类别。
有没有办法直接使用模板来执行此操作,以便选择每个项目(类别或产品)?我正在使用 MVVM 模式,不想求助于使用后面的代码,但如果有必要的话。