views:

2417

answers:

3

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.

A: 

First, you would set the ItemsSource of the TreeView to the DataSet.

Next you would set the ItemTemplate of the TreeView to the FirstLevelTemplate.

ItemTemplate="{StaticResource FirstLevelTemplate}"

The first template must reference the items that will use the second level template. Add an items source to this hierarchical template like so:

<HierarchicalDataTemplate x:Key="FirstLevelTemplate" ItemsSource="{Binding ChildItems}" ItemTemplate="{StaticResource SecondLevelTemplate}"/>

Change "ChildItems" to refer to the property within your DataSet that contains the child items. Repeat this for each HierarchicalDataTemplate.

Finally, you need to add controls to your DataTemplates in order to display the data. Here's a simple example:

<DataTemplate x:Key="FourthLevelTemplate"/>
   <Border BorderThickness="1" BorderBrush="Gray" CornerRadius="3">
      <TextBlock Text="{Binding DataProperty}"
                 HorizontalAlignment="Center" VerticalAlignment="Center"/>
   </Border>
</DataTemplate>

In this case, you would replace "DataProperty" with the property name of your data field. Note that you could add other controls (like TextBox, ComboBox, etc) to display additional data.

Josh G
Is there any way to set the control template of second level children?
Echilon
My suggestion was to create a HierarchicalDataTemplate for each level. The second level would be called something like "SecondLevelTemplate." The controls in this template would be displayed for the second level. If you want a "ControlTemplate", use Control in the DataTemplate and set the Template property on it.
Josh G
A: 

This site has some very good examples of using treeview.

BeaStollnitz

RBear
+1  A: 

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.

Denis Troller
I ended up creating a hierarchy of TreeViewItems programmatically, and that's seemed to work (although it's probably not the BEST practice). Thanks!
Pwninstein