After creating the TreeView in XAML and code, let us bind the TreeView to a collection.
We create a list of strings and set it as DataContext to TreeView:
List<string> items = new List<string>() {"Apple","Oracle","IBM","Microsoft" };
this.DataContext = items;
XAML:<TreeViewMargin="10,10"DockPanel.Dock="Right"Name="tvList"ItemsSource="{Binding}">
</TreeView>
Result is:
This is working like a ListBox, what we need to use is hierarchical data. Let’s create the classes which we will use in binding.
Class Category:
publicclass Category
{
publicstring Name
{ get; set; }
public List<string> ItemList
{ get; set; }
}
In the Window code-behind - we create two categories, add them to a list and set the list as the DataContext:
Category c1 = new Category { Name = "Software Companies", ItemList = new List<String>() { "Microsoft", "Oracle", } };
Cat…