Reputation: 47
I have a problem with binding a class to my TreeView
. It doesn't really work, I get nothing shown.
This is my class:
public class Main
{
public List<Child1> Ch1 { get; set; }
public List<Child2> Ch2 { get; set; }
}
public class Child1
{
public String CName { get; set; }
public List<Grandson> Grandson { get; set; }
public int Age { get; set; }
}
public class Grandson
{
public String GName { get; set; }
public List<Son> Son{ get; set; }
public List<Daught> Daught{ get; set; }
public int Age { get; set; }
}
public class Son
{
public String OName { get; set; }
public int Age { get; set; }
}
public class Daught
{
public String DName { get; set; }
public int Age { get; set; }
}
public class Child2
{
public String C2Name { get; set; }
public int Rights { get; set; }
}
As you see, I've got a lot of lists and lists in lists which I want to put in a TreeView.
<TreeView Grid.Row="1" ItemsSource="{Binding Main}" IsEnabled="{Binding TreeEnabled}" >
<TreeViewItem Header="{Binding CName}" ItemsSource="{Binding Ch1}" >
<TreeViewItem Header="{Binding GName}" ItemsSource="{Binding Grandson}" >
<TreeViewItem Header="{Binding OName}" ItemsSource="{Binding Son}" >
</TreeViewItem>
<TreeViewItem Header="{Binding DName}" ItemsSource="{Binding Daught}"></TreeViewItem>
</TreeViewItem>
</TreeViewItem>
<TreeViewItem Header="{Binding C2Name}" ItemsSource="{Binding Child2}"></TreeViewItem>
</TreeView>
I think I've got a problem with my bindings, but don't know what problem.
Upvotes: 0
Views: 171
Reputation:
First of all : you must made ObservableCollection <T>
when you want to bind a list to view , otherwise it wont show the changes to the view. (I do not know how familier you are with C# , then I say in <T>
, T means any type you are making a list of them. e.g. ObservableCollection <Person>
).
Second : If you bind the whole list to a treeview there is no need to bind one by one.
Third : Did you set your DataContext for you bindings any where ?
you need to do something like this in codebehind :
this.DataContext = new Class();
and then bind to this Class properties. This Class should be your main class in viewmodel.
Upvotes: 1
Reputation: 47
In my ViewModel i have a Instance of main also named main so it should bind it.
The Hierarchy is given i get i from the database the names are only examples.
The View must look like this
Upvotes: 0