TreeView
The TreeView
control can present hierarchical data and allows item selection. The items are templated so you can customise how they are displayed.
There are two data sources: the main items source for the control, this gives the root of the hierarchical data. Then there is the items source in the item template which allows the control to list the next level in the hierarchical data.
Useful Properties
You will probably use these properties most often:
Property | Description |
---|---|
ItemsSource | The bound collection that is used as the data source for the control. |
ItemsControl.ItemTemplate | The item template, contains a DataTemplate which will be applied to individual items and can be used to change how items look. |
ItemsControl.ItemPanel | The container panel to place items in. By default, this is a StackPanel. See this page to customise the ItemsPanel. |
ItemsControl.Styles | The style that is applied to any child element of the ItemControl. |
Example
This example uses a MVVM pattern view model to hold some hierarchical data based on a C# node class. In this example, there is a single root node in the Nodes
collection of the view model:
<TreeView ItemsSource="{Binding Nodes}">
<TreeView.ItemTemplate>
<TreeDataTemplate ItemsSource="{Binding SubNodes}">
<TextBlock Text="{Binding Title}"/>
</TreeDataTemplate>
</TreeView.ItemTemplate>
</TreeView>