使用具有不同嵌套类型的 HierarchicalDataTemplate [英] using HierarchicalDataTemplate with different nested types

查看:36
本文介绍了使用具有不同嵌套类型的 HierarchicalDataTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有大量关于 HierarchicalDataTemplate 的信息,但我很难找到足够具体的信息来帮助我处理包含不同类型的层次结构.

There seems to be a ton of information on HierarchicalDataTemplate, but I have had a hard time finding info specific enough to help me out with hierarchies containing different types.

假设类结构如下:

public class classA
{
    public string name{get;set;}
}
public class classB
{
    public string name{get;set;}
    public List<classA> subItems{get;set;}
}
public class classC
{
    public string name{get;set;}
    public List<classB> subItems{get;set;}
}

现在考虑到类不是自引用的原因,因此在我的层次结构中保持一种类型是包含它们的属性存在根本差异,有没有办法创建类型敏感的 HierarchicalDataTemplate?

Now taking into assumption the reason the classes aren't self referencing thus keeping one type throughout my hierarchy is that there are fundamental differences in properties contained theirin, is there a way to create a type sensitive HierarchicalDataTemplate?

推荐答案

HierarchicalDataTemplate 有一个 DataType 属性,所以你可以用它来指定类型,就像你做的一样数据模板.假设您将层次结构包装在视图模型中:

HierarchicalDataTemplate has a DataType property, so you use that to specify the type just as you do for DataTemplate. Let's say you wrap your hierarchy in a view model:

public class MyViewModel
{
    public List<classC> Items { get; set; }
}

然后像这样创建层次结构:

And then create your hierarchy like so:

this.DataContext = new MyViewModel
{
    Items = new List<classC>
    {
        new classC
        {
            name = "Class C",
            subItems = new List<classB> {
                new classB{
                    name = "Class B1",
                    subItems = new List<classA>{
                        new classA {name="Class A1a"},
                        new classA {name="Class A1b"},
                        new classA {name="Class A1c"},
                    }
                },
                new classB{
                    name = "Class B2",
                    subItems = new List<classA>{
                        new classA {name="Class A2a"},
                        new classA {name="Class A2b"},
                        new classA {name="Class A2c"},
                    }
                }
            }
        }
    }
};

然后在 XAML 中,您需要做的就是将相关的 DataTemplates 和 HierarchicalDataTemplates 添加到 TreeView 的资源块中:

Then in XAML all you need to do is add the relevant DataTemplates and HierarchicalDataTemplates to your TreeView's resources block:

<TreeView ItemsSource="{Binding Items}">
    <TreeView.Resources>

        <DataTemplate DataType="{x:Type local:classA}" >
            <TextBlock Text="{Binding name}" Foreground="Blue" />
        </DataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type local:classB}" ItemsSource="{Binding subItems}" >
            <TextBlock Text="{Binding name}" Foreground="Green" />
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type local:classC}" ItemsSource="{Binding subItems}" >
            <TextBlock Text="{Binding name}" Foreground="Red" />
        </HierarchicalDataTemplate>

    </TreeView.Resources>
</TreeView>

结果:

这篇关于使用具有不同嵌套类型的 HierarchicalDataTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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