WPF树视图 - 绑定的ViewModels使用嵌套的集合和"静态节点" [英] WPF TreeView - Binding to ViewModels with nested collections AND "Static nodes"

查看:172
本文介绍了WPF树视图 - 绑定的ViewModels使用嵌套的集合和"静态节点"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看了关于TreeView的数据绑定到嵌套集合了类似的问题,我读过一些有关此主题的乔希N衣的文章,但我的设计在不同我有静 TreeViewItems 充当可折叠容器嵌套的藏品。这是最好的说明我在寻找什么。

I've looked at the similar questions regarding TreeView data binding to nested collections, and I've read several "Josh 'n Bea" articles about this topic, but my design differs in that i have "static" TreeViewItems that serve as collapsible containers for the nested collection items. It's best to illustrate what I'm looking for.

鉴于这些视图模型类:

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; }
    }
}

这是我想要的结果:

任何人都可以帮助我的XAML这个TreeView的?我想我知道如何 HierarchicalDataTemplates 的工作,但静的容器节点(表,域,地图)在我看来迷惑。

Can anyone help me with the XAML for this TreeView? I thought i understood how HierarchicalDataTemplates work, but the "static" container nodes ("Tables", "Fields", "Maps") seem to confuse me.

感谢你,有一个愉快的一天!

Thank you and have a pleasant day!

推荐答案

我觉得你得创建 HierarchicalDataTemplate S为静态的节点也是如此。而且,由于一个的ItemsSource HierarchicalDataTemplate 需要一个集合,你可以在XAML中创建这些集合类似这样

I think you're gonna have to create HierarchicalDataTemplates for the "Static" nodes as well. And since ItemsSource of an HierarchicalDataTemplate expects a Collection you can create these collections in Xaml like this

命名空间

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作为的ItemsSource为 HierarchicalDataTemplate ,你不会有机会获得地图在一个新的水平集合属性,所以你将不得不爬上视觉树得到保持它像

The problem with this solution is that when you set e.g. MapCollection as ItemsSource for a HierarchicalDataTemplate, you won't have access to the Maps Collection Property in the next level so you'll have to climb up the Visual Tree to get a hold of it like

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}"/>

这篇关于WPF树视图 - 绑定的ViewModels使用嵌套的集合和&QUOT;静态节点&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆