Gavin Coates
Gavin Coates

Reputation: 1425

Silverlight TreeView Wont Populate

I am trying to populate a TreeView in silverlight, but I am having some difficulties - it always appears empty.

I found a good tutorial (here) on how to do it, I created a new project and copied the code as it appears, using C#, and it worked perfectly. I have now tried to incorporate it into my project (which uses VB), but It doesnt work. My VB code is as follows:

XAML

<UserControl.Resources>
    <common:HierarchicalDataTemplate x:Key="myHierarchicalTemplate" ItemsSource="{Binding Items}" >
        <TextBlock Text="{Binding myString}" />
    </common:HierarchicalDataTemplate>
</UserControl.Resources>

<controls:TreeView Height="200" HorizontalAlignment="Left" Margin="280,464,0,0" Name="TreeView1" VerticalAlignment="Top" Width="120" ItemTemplate="{StaticResource myHierarchicalTemplate}" />

VB Code:

Public Class myItem

Public myString As String
Public Items As ObservableCollection(Of myItem)

Public Sub New(ByVal myString As String, ByVal ParamArray myItems() As myItem)
    Me.myString = myString

    Dim itemsObservableCollection = New ObservableCollection(Of myItem)

    For Each item As myItem In myItems
        itemsObservableCollection.Add(item)
    Next

    Me.Items = itemsObservableCollection

End Sub
End Class

And to populate it:

Dim itemsSource As New ObservableCollection(Of myItem)

    itemsSource.Add(New myItem("Hello", New myItem("World"),
                                       New myItem("Foo")))
    itemsSource.Add(New myItem("Moo", New myItem("Boo", New myItem("Goo"))))




    TreeView1.ItemsSource = itemsSource

What am I missing?

Edit: I have tried debugging the project, and the "itemsSource" collection appears to be generating correctly, and is being assigned to the TreeView1.ItemsSource fine, but nothing is displaying. Is it a display binding issue?

Upvotes: 0

Views: 405

Answers (1)

Scott
Scott

Reputation: 209

To use bindings you have to expose properties- while my VB.NET isn't great it looks like Items is a field not a property in your myItem class, and the same for myString.

Cheers -

Upvotes: 4

Related Questions