If I have a DataSet as a result of a SQL query, can I bind it directly to a TreeView and show the hierarchy of my data? I know I’d have to use several HierarchicalDataTemplates, but I don’t know how to tell each one what data to display.
If I have a 4-level hierarchy, like so:
<HierarchicalDataTemplate x:Key='FirstLevelTemplate' ItemTemplate='{StaticResource SecondLevelTemplate}'/> <HierarchicalDataTemplate x:Key='SecondLevelTemplate' ItemTemplate='{StaticResource ThirdLevelTemplate}'/> <HierarchicalDataTemplate x:Key='ThirdLevelTemplate' ItemTemplate='{StaticResource FourthLevelTemplate}'/> <DataTemplate x:Key='FourthLevelTemplate'/>
What property(ies) need to be set to display my data directly from a DataSet?
edit: Ideally, I’d like to do this using a single self-referencing DataTable.
I do not think you can do that.
The Treeview control, and the HierarchicalDataTemplate expect a hierarchy of objects. The DataSet is inherently flat.
You will have to somehow convert that dataset to a hierarchy of objects, each with its own ‘Children’ collection. The ItemsSource of the treeview will be bound to the ‘top level collection’ (the rows without a parent reference).
Each HierarchicalDataTemplate will have its ItemsSource property bound to the corresponding Children property.
There might be a solution using converters, but if it exists it would probably end up being more complicated than straight up reshaping the data before binding.