I have following problem with which I'm wrestling now for about 5 hours.
How do I correctly bind the following class structure to a TreeView?
public class Person
{
public string Display { get; set; }
public List<Car> Cars { get; set; }
public List<House> Houses { get; set; }
}
public class Car
{
public string Display { get; set; }
}
public class House
{
public string Display { get; set; }
}
I want the TreeView to have the following structure: (Where Person #1 has both houses and cars, and Person #2 just has houses)
- Person #1
|-- Cars
|-- Car #1
|-- Car #2
|-- Houses
|-- House #1
- Person #2
|-- Houses
|-- House #1
|-- House #2
How on earth do you this in XAML with automatic Databinding to an ObservableCollection? I set the binding only once in the Constructor of my Treeview (UserControl) like this:
this.ItemsSource = SomeStaticClass.People;
Help is very very much appreciated! Thanks! =)
EDIT: I tried the HierarchicalDataTemplate, but I can't get it to work ;)
EDIT2: I tried many many different things, but the closest I have come to is following code (with just one Enumeration in mind at this time ;)):
<HierarchicalDataTemplate DataType="{x:Type e:Person}" ItemsSource="{Binding Cars}">
<TextBlock Text="{Binding Display}" />
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Cars}">
<TextBlock Text="Cars" />
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Display}" />
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
Unfortunately, it doesn't display the TextBlock Cars and the Cars themself. How do I do that?
HierarchicalDataTemplate
? – Jake Berger