--- title: "How to: Bind a TreeView to Data That Has an Indeterminable Depth" ms.date: "03/30/2017" helpviewer_keywords: - "TreeView control [WPF], binding to data of indeterminate depth" ms.assetid: daddcd74-1b0f-4ffd-baeb-ec934c5e0f53 --- # How to: Bind a TreeView to Data That Has an Indeterminable Depth There might be times when you want to bind a to a data source whose depth is not known. This can occur when the data is recursive in nature, such as a file system, where folders can contain folders, or a company's organizational structure, where employees have other employees as direct reports. The data source must have a hierarchical object model. For example, an `Employee` class might contain a collection of Employee objects that are the direct reports of an employee. If the data is represented in a way that is not hierarchical, you must build a hierarchical representation of the data. When you set the property and if the generates an for each child item, then the child uses the same as the parent. For example, if you set the property on a data-bound , each that is generated uses the that was assigned to the property of the . The enables you to specify the for a , or any , on the data template. When you set the property, that value is used when the is applied. By using a , you can recursively set the for each in the . ## Example The following example demonstrates how to bind a to hierarchical data and use a to specify the for each . The binds to XML data that represents the employees in a company. Each `Employee` element can contain other `Employee` elements to indicate who reports to whom. Because the data is recursive, the can be applied to each level. [!code-xaml[TreeViewWithUnknownDepth#1](~/samples/snippets/csharp/VS_Snippets_Wpf/TreeViewWithUnknownDepth/CS/Window1.xaml#1)] ## See also - [Data Binding Overview](../data/data-binding-overview.md) - [Data Templating Overview](../data/data-templating-overview.md)