XAML Code for TREEVIEW

前端 未结 1 1278
遇见更好的自我
遇见更好的自我 2021-01-26 10:25

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         


        
1条回答
  •  北海茫月
    2021-01-26 11:06

    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

    • Create the myObservableCollection private field of the type ObservableCollection(Of TreeNodeSet).
    • Add to the constructor the line DisksTreeView1.ItemsSource = myObservableCollection
    • Change the line DisksTreeView1.Items.Add(pnode) to the line myObservableCollection.Add(t).
    • Add the Disks property to the TreeNodeSet class (the type is ObservableCollection too)
    • In the xaml replace the line with DataTemplate to the line
    • Change 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") 
    

    0 讨论(0)
提交回复
热议问题