Question

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

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

The ClassPupils should hold all pupils in a class.

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>

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

Was it helpful?

Solution

ItemsSource for HierarchicalDataTemplate should be Pupils.

It should point to the collection containing childs for this dataTemplate which is Pupils and not Class.

<HierarchicalDataTemplate DataType="{x:Type model:ClassPupils}"
                           ItemsSource="{Binding Pupils}">
    <Label Content="{Binding Class.ClassName}"/>
</HierarchicalDataTemplate>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top