mardi 5 mai 2015

Using WPF TreeView to create a hierarchy

I am trying to use WPF TreeView to create a hierarchy in my application, I am trying to create a hierarchy as follows:

Obj A
-Obj B
--Obj C
---Obj D
----Obj E
---Obj D
----Obj E

In essence each of the objects in the hierarchy above with different letters are different types of objects which all inherit from an abstract class to share common properties.

One of this these common properties is a List which contains the children of that particular node.

After browsing some online tutorials and forums, I have had some difficulty in creating the hierarchy using HierarchicalDataTemplate XAML attributes.

At the moment I am using the following XAML:

<TreeView x:Name="treeviewProjectStructure" Grid.Row="1" Margin="2" SelectedItemChanged="treeviewProjectStructure_SelectedItemChanged" ItemsSource="{Binding Nodes}">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:Node}" ItemsSource="{Binding Children.Name}">
                <TextBlock Text="{Binding Name}"></TextBlock>
            </HierarchicalDataTemplate>
        </TreeView.Resources>
</TreeView>

In the example above "Nodes" is a ObservableCollection which holds all the nodes which is declared in the MainWindow.cs

Using this XAML simply displays the root node without any children nodes.

Any help would be much appreciated, thanks :)

Aucun commentaire:

Enregistrer un commentaire