Question

In a previous question I wanted to know, how to set up the HierarchicalDataTemplate for a TreeView. This just works fine. My problem now is the following:

The class ClassPupils 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; }
}

In my ViewModel I have an ObservableCollection<ClassPupils> where the TreeView is bound to.

If a ClassPupil has no Pupils in the Pupils-Collection, the item is not displayed. I don't understand why? I need to also display ClassPupils with no Pupils in the Pupils-Collection.

Was it helpful?

Solution

Your data model is incorrect. Our data models in code often do not exactly follow that of our database structure, especially when the data is hierarchical... remember, our tables are normalised, whereas there is no benefit from doing this in code. So your database table may have the foreign key Id of a Class in a child table, but in a business model, this is usually implemented as a parent class that has a collection of children. So in your case, your parent class should look more like this:

public class SchoolClass
{
    public SchoolClass(List<Pupil> pupils)
    {
        this.Pupils = pupils;
    }

    public List<Pupil> Pupils { get; set; }

    ...
}

There is no need to define a 'weak entity' or 'joining table' in code. Whereas your problem before was that your parent SchoolClass class did not have a collection of Pupils, now it does, so try your HierarchicalDataTemplate again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top