--- title: "TreeView Overview" description: Learn how the Windows Presentation Foundation TreeView control displays information in a hierarchical structure by using nodes, including simple examples. ms.date: "03/30/2017" helpviewer_keywords: - "expanding node [WPF]" - "TreeView control [WPF], about TreeView control" - "Control class [WPF], TreeView" ms.assetid: 62212512-5a5c-4864-949e-b6a6a3a52c02 --- # TreeView Overview The control provides a way to display information in a hierarchical structure by using collapsible nodes. This topic introduces the and controls, and provides simple examples of their use. ## What Is a TreeView? is an that nests the items by using controls. The following example creates a . [!code-xaml[TreeViewSnips#EmbeddedTVIs](~/samples/snippets/csharp/VS_Snippets_Wpf/TreeViewSnips/CSharp/Window1.xaml#embeddedtvis)] ## Creating a TreeView The control contains a hierarchy of controls. A control is a that has a and an collection. If you are defining a by using [!INCLUDE[TLA#tla_xaml](../../../includes/tlasharptla-xaml-md.md)], you can explicitly define the content of a control and the items that make up its collection. The previous illustration demonstrates this method. You can also specify an as a data source and then specify a and to define the content. To define the layout of a control, you can also use objects. For more information and an example, see [Use SelectedValue, SelectedValuePath, and SelectedItem](how-to-use-selectedvalue-selectedvaluepath-and-selecteditem.md). If an item is not a control, it is automatically enclosed by a control when the control is displayed. ## Expanding and Collapsing a TreeViewItem If the user expands a , the property is set to `true`. You can also expand or collapse a without any direct user action by setting the property to `true` (expand) or `false` (collapse). When this property changes, an or event occurs. When the method is called on a control, the and its parent controls expand. If a is not visible or partially visible, the scrolls to make it visible. ## TreeViewItem Selection When a user clicks a control to select it, the event occurs, and its property is set to `true`. The also becomes the of the control. Conversely, when the selection changes from a control, its event occurs and its property is set to `false`. The property on the control is a read-only property; hence, you cannot explicitly set it. The property is set if the user clicks on a control or when the property is set to `true` on the control. Use the property to specify a of a . For more information, see [Use SelectedValue, SelectedValuePath, and SelectedItem](how-to-use-selectedvalue-selectedvaluepath-and-selecteditem.md). You can register an event handler on the event in order to determine when a selected changes. The that is provided to the event handler specifies the , which is the previous selection, and the , which is the current selection. Either value can be `null` if the application or user has not made a previous or current selection. ## TreeView Style The default style for a control places it inside a object that contains a control. When you set the and properties for a , these values are used to size the object that displays the . If the content to display is larger than the display area, a automatically displays so that the user can scroll through the content. To customize the appearance of a control, set the property to a custom . The following example shows how to set the and property values for a control by using a . [!code-xaml[TreeViewSimple#8](~/samples/snippets/csharp/VS_Snippets_Wpf/TreeViewSimple/CS/Window1.xaml#8)] ## Adding Images and Other Content to TreeView Items You can include more than one object in the content of a . To include multiple objects in content, enclose the objects inside a layout control, such as a or . The following example shows how to define the of a as a and that are both enclosed in a control. [!code-xaml[TreeViewSnips#TVIHeader](~/samples/snippets/csharp/VS_Snippets_Wpf/TreeViewSnips/CSharp/Window1.xaml#tviheader)] The following example shows how to define a that contains an and a that are enclosed in a control. You can use a to set the or for a . [!code-xaml[TreeViewDataBinding#6](~/samples/snippets/csharp/VS_Snippets_Wpf/TreeViewDataBinding/CSharp/Window1.xaml#6)] ## See also - - - [How-to Topics](treeview-how-to-topics.md) - [WPF Content Model](wpf-content-model.md)