# .NET and More > WPF, WCF, WF >  [RESOLVED] WPF - Binding a treeview to data objects

## keystone_paul

Hi there - hope someone can help. I've been looking for examples in books and online and I just can't find anything that gets me moving forward. I've been working with traditional forms in VB since VB3 but I'm trying to get my head around WPF and MVVM but I can't for the life of me get it to do what would take 30 seconds in winforms.

I have a hierarchical structure of geographical locations so at the top level is an instance of a Location object representing "The World". This Location object also has a locations collection which has for example "Europe" and "North America" and in Europe's locations collection there might be "UK", "France", "Germany" etc.

How do I throw that collection of location objects at a treeview and make it draw the whole tree?

At the moment the best I can do is set the ItemsSource of the treeview to the "World" instance of Location, however that doesn't list any child nodes (just top level nodes, ie "World").

All the examples I can find so far for WPF treeviews rely on just XAML with a hardcoded set of nodes, or binding to a database which I'm not using.

Any pointers would be much appreciated. I'm sure its really simple but just a question of getting my head around binding in WPF.

Thanks in advance

----------


## keystone_paul

A heavily mocked up version of the underlying object :



```
Public Class LocationManager

    Private _Locations As List(Of Location)

    Public Sub New()

        _Locations = New List(Of Location)

        'Create World location
        Dim World As New Location("World")
        World.Description = "The Whole Wide World!"

        'Create non-World location
        Dim Moon As New Location("Moon")
        Moon.Description = "The Moon!"

        'Create a child location and add to the world
        Dim Europe As New Location("Europe", World)
        Europe.Description = "Just Europe"
        World.Locations.Add(Europe)

        _Locations.Add(World)
        _Locations.Add(Moon)


    End Sub

    Public Property Locations As List(Of Location)
        Get
            Return _Locations
        End Get
        Set(ByVal value As List(Of Location))
            _Locations = value
        End Set
    End Property

End Class
```


My ViewModel class (which is largely redundant at this point) :



```
Public Class vmLocations

    Private _LocationManager As LocationManager

    Public Sub New(ByRef MyLocationManager As LocationManager)

        _LocationManager = MyLocationManager

    End Sub

    Public ReadOnly Property Caption As String
        Get
            Return "Dummy Caption"
        End Get
    End Property
    Public Property Locations As List(Of Location)
        Get
            Return _LocationManager.Locations
        End Get
        Set(ByVal value As List(Of Location))
            _LocationManager.Locations = value
        End Set
    End Property

End Class
```



Finally my XAML :



```
<Window x:Class="vLocations"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="vLocations" Height="300" Width="300">
    <Grid>
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="30,12,0,0" Name="TextBlock1" Text="{Binding Caption}" VerticalAlignment="Top" Width="220" />
        <TreeView Margin="32,38,28,23" Name="tvwLocations" ItemsSource="{Binding Locations}">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="Location">
                <TextBlock Text="{Binding Name}"/>
            </HierarchicalDataTemplate>
            </TreeView.Resources>
        </TreeView>
    </Grid>
</Window>
```



And the code-behind



```
Public Class vLocations

    Private _LocationsViewModel As OspreyBusinessObjects.vmLocations

    Public Sub New(ByVal vmLocations As OspreyBusinessObjects.vmLocations)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        _LocationsViewModel = vmLocations

        DataContext = _LocationsViewModel

        Me.Show()

    End Sub
End Class
```

----------


## jmcilhinney

This was the third match in a search for _wpf bind treeview_:

http://www.codeproject.com/KB/WPF/Tr...ViewModel.aspx

I haven't examined it closely but it seems to be relevant.

I've asked the mods to move this thread to the WPF forum, where it belongs.

_[It has been done  mod]_

----------


## keystone_paul

Thanks jmcilhinney - thats actually a page that I've been trying to get my head around. Perhaps its because thats in C# which I don't have so much experience with, but I can't seem to relate that to my solution.

I guess I'll have to go back and read through it again.

----------


## keystone_paul

Sorted. Had the HierarchicalItemTemplate in the wrong place (wasnt within an ItemTemplate element)

I find WPF frustrating to debug - it seems that there is a lot less information fed back but I guess thats something I just have to get used to!

----------


## jmcilhinney

I haven't used it myself as yet but this might help in that regard:

http://wpfinspector.codeplex.com/

----------


## keystone_paul

Thanks jmcilhinney - that looks like it could be really useful. I've just had a quick play and I think it will make diagnosing issues a lot easier!

----------

