Binding hierarchical data to a TreeView
A TreeView
is a common control for viewing hierarchical data. It derives from ItemsControl
, but it's a bit more
complex than standard controls derived from, ItemsControl
, such as ListBox
, because of its hierarchical nature, although it's possible to use a TreeView
without data binding. Binding provides an elegant way to fill the tree with data. Let's examine this in more detail.
Getting ready
Make sure Visual Studio is up and running.
How to do it...
We'll create an application that shows a tree view with processes as the top level objects. For each process, child tree view items will show the modules loaded into that process.
Create a new WPF Application named
CH06.TreeViewBinding
.Open
MainWindow.xaml.cs
. In the constructor, set theDataContext
to the collection of processes on the system:DataContext = Process.GetProcesses();
We'd like to show a
TreeView
of processes with each tree node expanding to show all modules loaded into that process. OpenMainWindow...