好吧,我现在还不明白。我搜索google和Stackoverflow有一段时间了,并且看到了很多如何使用HierarchicalDataTemplate的例子
我有一个名为Class的类,它看起来像:
[Table(Name = "Class")]
public class Class
{
[Column(IsDbGenerated = true, IsPrimaryKey = true)]
public int Id { get; private set; }
[Column]
public string ClassName { get; set; }
}我有一个名为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; }
}我介绍了一个名为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; }
}ClassPupils应该把所有的学生都关在一个班级里。
现在,我想创建一个TreeView,其中所有的Pupil都列在Class下面。为此,我在ViewModel中使用了一个ViewModel。在我看来,我是绑定到这个集合。“我的视图- 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>但我只得到TreeViewItems的班级,而不是学生-Lastname。我做错了什么?
发布于 2014-02-09 12:55:32
ItemsSource for HierarchicalDataTemplate应该是Pupils。
它应该指向包含这个dataTemplate的子类集合,它是Pupils而不是Class。
<HierarchicalDataTemplate DataType="{x:Type model:ClassPupils}"
ItemsSource="{Binding Pupils}">
<Label Content="{Binding Class.ClassName}"/>
</HierarchicalDataTemplate>https://stackoverflow.com/questions/21659253
复制相似问题