HierarchicalDataTemplate的树视图 [英] HierarchicalDataTemplate for TreeView

查看:222
本文介绍了HierarchicalDataTemplate的树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我不会在那一刻得到它。我搜索谷歌和#1一段时间,看到的例子如何使用 HierarchicalDataTemplate

Ok, I don't get it at the moment. I've searched google and Stackoverflow for a while and saw lot's of examples how to use the HierarchicalDataTemplate

我有很多的一个名为类,它是这样的:

I have a class called Class which looks like:

[Table(Name = "Class")]
public class Class
{
    [Column(IsDbGenerated = true, IsPrimaryKey = true)]
    public int Id { get; private set; }

    [Column]
    public string ClassName { get; set; }
}

和我有一个名为瞳孔它看起来像:

And I have a class called Pupil which looks like:

[Table(Name = "Pupil")]
public class Pupil
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true)]
    public int Id { get; private set; }

    public Name Name
    {
        get { return this.nameEntityRef.Entity; }
        set
        {
            this.nameEntityRef.Entity = value;
            this.NameId = value.Id;
        }
    }

    public Address Address
    {
        get { return this.addressEntityRef.Entity; }
        set
        {
            this.addressEntityRef.Entity = value;
            this.AddressId = value.Id;
        }
    }

    public Class Class
    {
        get { return this.classEntityRef.Entity; }
        set
        {
            this.classEntityRef.Entity = value;
            this.ClassId = value.Id;
        }
    }

    private EntityRef<Name> nameEntityRef;
    private EntityRef<Address> addressEntityRef;
    private EntityRef<Class> classEntityRef;

    [Column]
    internal int AddressId { get; set; }

    [Column]
    internal int NameId { get; set; }

    [Column]
    internal int ClassId { get; set; }
}

和我引入了一个名为类 ClassPupils 它看起来像

And I introduced a class called ClassPupils which looks like

public class ClassPupils
{
    public ClassPupils(Class @class)
    {
        this.Class = @class;
        this.Pupils = new List<Pupil>();
    }

    public Class Class { get; set; }
    public List<Pupil> Pupils { get; set; }
}



ClassPupils 应持有所有学生中的一类。

The ClassPupils should hold all pupils in a class.

现在我想创建一个 TreeView控件,所有瞳孔是S有上市。在我的ViewModel ;为此我使用的ObservableCollection< ClassPupils过夜。在我看来,我结合这个集合。我查看代码以TreeView的样子:

Now I want to create a TreeView where all Pupils listed under there Class. Therefor I use an ObservableCollection<ClassPupils> in my ViewModel. In my View I'm binding to this collection. My View-Code for the TreeView looks like:

<TreeView Grid.Row="0" Grid.Column="0" Margin="2" ItemsSource="{Binding ClassPupils}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type model:ClassPupils}" ItemsSource="{Binding Class}">
            <Label Content="{Binding Class.ClassName}"/>
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type entity:Pupil}">
            <Label Content="{Binding Name.Lastname}"/>
        </DataTemplate>
    </TreeView.Resources>
</TreeView>



不过,我只得到了类而不是为学生,姓的TreeViewItems。我在做什么错了?

But I only get the TreeViewItems for the Classes and not for the Pupils-Lastname. What am I doing wrong?

推荐答案

的ItemsSource 为HierarchicalDataTemplate应学生

ItemsSource for HierarchicalDataTemplate should be Pupils.

这应该 指向包含孩子的这个DataTemplate中<收集/ EM> 学生,而不是

<HierarchicalDataTemplate DataType="{x:Type model:ClassPupils}"
                           ItemsSource="{Binding Pupils}">
    <Label Content="{Binding Class.ClassName}"/>
</HierarchicalDataTemplate>

这篇关于HierarchicalDataTemplate的树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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