A heavily mocked up version of the underlying object :
Code:
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) :
Code:
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 :
Code:
<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
Code:
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