Following is the code I wrote for generating treeview hierarchy,
For Each k As KeyValuePair(Of String, GenreSet) In GenreSetDictionary
Dim t As New T
I see few minor mismatches here: the name of the TreeView
control (TreeView1 or DisksTreeView1) and the ImagePath
property (or Imagepath
, c# is sensible to the register of variables).
But the main reason of the incorrect behavior is that the ItemTemplate
property is applied to the ItemsSource
property, not to the Items
property.
Here are two possible ways to correct the code:
1) Fixeing of the data class, item template and binding to the ItemsSource
myObservableCollection
private field of the type ObservableCollection(Of TreeNodeSet)
.DisksTreeView1.ItemsSource = myObservableCollection
DisksTreeView1.Items.Add(pnode)
to the line myObservableCollection.Add(t)
.Disks
property to the TreeNodeSet
class (the type is ObservableCollection
too)DataTemplate
to the line
pnode.Items.Add(cnode)
to the line t.Disks.Add(tt)
.2) Using the HeaderTemplate
property instead of the ItemTemplate
property.
At first, move the DataTemplate to resources and add some key. Then add a similar code near each TreeViewItem
in the code-behind:
Dim pnode As New TreeViewItem
pnode.DataContext = t
pnode.Header = t
pnode.HeaderTemplate = Resources("someKey")