简体   繁体   English

TreeView的HierarchicalDataTemplate

[英]HierarchicalDataTemplate for TreeView

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 我已经搜索了一段时间的Google和Stackoverflow,并看到了很多如何使用HierarchicalDataTemplate的示例。

I have a class called Class which looks like: 我有一个叫做Class ,它看起来像:

[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: 我有一个叫做Pupil的类,看起来像:

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

And I introduced a class called ClassPupils which looks like 我介绍了一个叫做ClassPupils

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

The ClassPupils should hold all pupils in a class. ClassPupils应该让所有学生都上一堂课。

Now I want to create a TreeView where all Pupil s listed under there Class . 现在,我想创建一个TreeView ,其中所有Pupil都在Class Therefor I use an ObservableCollection<ClassPupils> in my ViewModel. 因此,我在ViewModel中使用了ObservableCollection<ClassPupils> In my View I'm binding to this collection. 在我的视图中,我绑定到此集合。 My View-Code for the TreeView looks like: 我对TreeView的查看代码如下所示:

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

But I only get the TreeViewItems for the Classes and not for the Pupils-Lastname. 但是我只为该类而不是学生姓氏获取TreeViewItems。 What am I doing wrong? 我究竟做错了什么?

ItemsSource for HierarchicalDataTemplate should be Pupils . HierarchicalDataTemplate的ItemsSource应该是Pupils

It should point to the collection containing childs for this dataTemplate which is Pupils and not Class . 它应该指向包含孩子的这个DataTemplate中收集 Pupils ,而不是Class

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM