首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HierarchicalDataTemplate for TreeView

HierarchicalDataTemplate for TreeView
EN

Stack Overflow用户
提问于 2014-02-09 12:53:45
回答 1查看 232关注 0票数 2

好吧,我现在还不明白。我搜索google和Stackoverflow有一段时间了,并且看到了很多如何使用HierarchicalDataTemplate的例子

我有一个名为Class的类,它看起来像:

代码语言:javascript
复制
[Table(Name = "Class")]
public class Class
{
    [Column(IsDbGenerated = true, IsPrimaryKey = true)]
    public int Id { get; private set; }

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

我有一个名为Pupil的类,它看起来像:

代码语言:javascript
复制
[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的类,它看起来像

代码语言:javascript
复制
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应该把所有的学生都关在一个班级里。

现在,我想创建一个TreeView,其中所有的Pupil都列在Class下面。为此,我在ViewModel中使用了一个ViewModel。在我看来,我是绑定到这个集合。“我的视图- TreeView代码”如下所示:

代码语言:javascript
复制
<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的班级,而不是学生-Lastname。我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-09 12:55:32

ItemsSource for HierarchicalDataTemplate应该是Pupils

它应该指向包含这个dataTemplate的子类集合,它是Pupils而不是Class

代码语言:javascript
复制
<HierarchicalDataTemplate DataType="{x:Type model:ClassPupils}"
                           ItemsSource="{Binding Pupils}">
    <Label Content="{Binding Class.ClassName}"/>
</HierarchicalDataTemplate>
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21659253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档