![]() ![]() Because the data is recursive, the HierarchicalDataTemplate can be applied to each level. Each Employee element can contain other Employee elements to indicate who reports to whom. The TreeView binds to XML data that represents the employees in a company. The following example demonstrates how to bind a TreeView to hierarchical data and use a HierarchicalDataTemplate to specify the ItemsSource for each TreeViewItem. By using a HierarchicalDataTemplate, you can recursively set the ItemsSource for each TreeViewItem in the TreeView. When you set the HierarchicalDataTemplate.ItemsSource property, that value is used when the HierarchicalDataTemplate is applied. The HierarchicalDataTemplate enables you to specify the ItemsSource for a TreeViewItem, or any HeaderedItemsControl, on the data template. ![]() ![]() For example, if you set the ItemTemplate property on a data-bound TreeView, each TreeViewItem that is generated uses the DataTemplate that was assigned to the ItemTemplate property of the TreeView. When you set the ItemsControl.ItemTemplate property and if the ItemsControl generates an ItemsControl for each child item, then the child ItemsControl uses the same ItemTemplate as the parent. If the data is represented in a way that is not hierarchical, you must build a hierarchical representation of the data. For example, an Employee class might contain a collection of Employee objects that are the direct reports of an employee. The source code available with this article is a complete working sample. Other than the XML file itself, almost all of the work to enable this is done within XAML, and most of that is done by defining several resources. The data source must have a hierarchical object model. It is actually fairly easy to display XML data using WPF. 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. There might be times when you want to bind a TreeView to a data source whose depth is not known. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |