我已经查看了有关 TreeView 数据绑定(bind)到嵌套集合的类似问题,并且我已经阅读了几篇关于此主题的“Josh 'n Bea”文章,但我的设计不同之处在于我有“静态”TreeViewItems
用作嵌套集合项的可折叠容器。最好说明我在寻找什么。
鉴于这些 ViewModel 类:
namespace TreeViewSample
{
public class ApplicationViewModel
{
public ApplicationViewModel() { Projects = new List<ProjectViewModel>(); }
public IEnumerable<ProjectViewModel> Projects { get; set; }
}
public class ProjectViewModel
{
public ProjectViewModel() { Maps = new List<MapViewModel>(); }
public string Name { get; set; }
public IEnumerable<MapViewModel> Maps { get; set; }
}
public class MapViewModel
{
public MapViewModel() { Tables = new List<TableViewModel>(); }
public string Name { get; set; }
public IEnumerable<TableViewModel> Tables { get; set; }
}
public class TableViewModel
{
public TableViewModel() { Fields = new List<FieldViewModel>(); }
public string Name { get; set; }
public IEnumerable<FieldViewModel> Fields { get; set; }
}
public class FieldViewModel
{
public string Name { get; set; }
}
}
这是我想要的结果:
任何人都可以帮我处理这个 TreeView 的 XAML 吗?我以为我明白了
HierarchicalDataTemplates
工作,但“静态”容器节点(“表”、“字段”、“ map ”)似乎让我感到困惑。谢谢你,祝你有愉快的一天!
最佳答案
我认为你将不得不创建 HierarchicalDataTemplate
s 也适用于“静态”节点。并且由于 HierarchicalDataTemplate
的 ItemsSource期望一个集合,您可以像这样在 Xaml 中创建这些集合
命名空间
xmlns:coll="clr-namespace:System.Collections;assembly=mscorlib"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
收藏品
<coll:ArrayList x:Key="MapCollection">
<sys:String>Maps</sys:String>
</coll:ArrayList>
<coll:ArrayList x:Key="TableCollection">
<sys:String>Tables</sys:String>
</coll:ArrayList>
<coll:ArrayList x:Key="FieldCollection">
<sys:String>Fields</sys:String>
</coll:ArrayList>
此解决方案的问题在于,当您设置例如MapCollection 作为
HierarchicalDataTemplate
的 ItemsSource ,您将无法访问 Maps
下一个级别的集合属性,因此您必须爬上可视树才能捕获它ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}},
Path=DataContext.Maps}"
使用这种方法,您的
HierarchicalDataTemplate
s 可以是这样的<!-- Field Templates -->
<HierarchicalDataTemplate x:Key="FieldsTemplate">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="FieldTemplate"
ItemTemplate="{StaticResource FieldsTemplate}"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}},
Path=DataContext.Fields}">
<TextBlock Text="{Binding}"/>
</HierarchicalDataTemplate>
<!-- Table Templates -->
<HierarchicalDataTemplate x:Key="TablesTemplate"
ItemTemplate="{StaticResource FieldTemplate}"
ItemsSource="{Binding Source={StaticResource FieldCollection}}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="TableTemplate"
ItemTemplate="{StaticResource TablesTemplate}"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}},
Path=DataContext.Tables}">
<TextBlock Text="{Binding}"/>
</HierarchicalDataTemplate>
<!-- Map Templates -->
<HierarchicalDataTemplate x:Key="MapsTemplate"
ItemTemplate="{StaticResource TableTemplate}"
ItemsSource="{Binding Source={StaticResource TableCollection}}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="MapTemplate"
ItemTemplate="{StaticResource MapsTemplate}"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}},
Path=DataContext.Maps}">
<TextBlock Text="{Binding}"/>
</HierarchicalDataTemplate>
<!-- Project Template -->
<HierarchicalDataTemplate x:Key="ProjectDataTemplate"
ItemTemplate="{StaticResource MapTemplate}"
ItemsSource="{Binding Source={StaticResource MapCollection}}">
<TextBlock Text="{Binding Path=Name}"/>
</HierarchicalDataTemplate>
TreeView
<TreeView Name="treeView"
ItemTemplate="{StaticResource ProjectDataTemplate}"
ItemsSource="{Binding Projects}"/>
关于.net - WPF TreeView - 使用嵌套集合和 "Static nodes"绑定(bind)到 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4980392/