问题描述
好吧,我不会在那一刻得到它。我搜索谷歌和#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; }
}
和我有一个名为瞳孔班code>它看起来像:
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 Pupil
s 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中 是学生
,而不是类
。
<HierarchicalDataTemplate DataType="{x:Type model:ClassPupils}"
ItemsSource="{Binding Pupils}">
<Label Content="{Binding Class.ClassName}"/>
</HierarchicalDataTemplate>
这篇关于HierarchicalDataTemplate的树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!