TOC

This article is currently in the process of being translated into Spanish (~22% done).

The TreeView control:

TreeView introduction

La función de TreeView Control (vista de árbol), te permite mostrar en pantalla datos de jerarquías, en donde cada pieza de datos es representada por un nodo en el árbol. Cada nodo puede tener hijos y cada hijo puede tener otro hijo y así sucesivamente. Si alguna vez has usado el buscador de windows, entonces sabrás como se ve una vista de árbol - es el menú que muestra el folder actual en la estructura de tu equipo en la parte izquierda de "mi equipo" o "mi PC".

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 hard to 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 much effort.

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 possible. But 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.

Summary

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.

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!