The TreeView control enabled you to display hierarchical data, with
each piece of data represented by a node in the tree. Each node can then
nodes, and the child nodes can have child nodes and so on. If you
have ever used the Windows Explorer, you also know how a TreeView looks -
control that shows the current folder structure on your machine, in
the left part of the Windows Explorer window.
TreeView in WPF vs. WinForms
If you have previously worked with the TreeView control in WinForms,
you might think of the TreeView control as one that's easy to use but
customize. In WPF it's a little bit the other way around, at least
for newbies: It feels a bit complicated to get started with, but it's a
LOT easier to
customize. Just like most other WPF controls, the TreeView is almost
lookless once you start, but it can be styled almost endlessly without
Just like with the ListView control, the TreeView control does have
its own item type, the TreeViewItem, which you can use to populate the
TreeView. If you
come from the WinForms world, you will likely start by generating
TreeViewItem's and adding them to the Items property, and this is indeed
since this is WPF, the preferred way is to bind the TreeView to a
hierarchical data structure and then use an appropriate template to
render the content.
We'll show you how to do it both ways, and while the good, old
WinForms inspired way might seem like the easy choice at first, you
should definitely give
the WPF way a try - in the long run, it offers more flexibility and
will fit in better with the rest of the WPF code you write.
The WPF TreeView is indeed a complex control. In the first example,
which we'll get into already in the next chapter, it might seem simple,
but once you
dig deeper, you'll see the complexity. Fortunately, the WPF TreeView
control rewards you with great usability and flexibility. To show you
all of them,
we have dedicated an entire category to all the TreeView articles.
Click on to the next one to get started.