开发者

TreeView nodes not being displayed [duplicate]

开发者 https://www.devze.com 2023-03-06 06:52 出处:网络
This question already has an answer here: Closed 10 years ago. Possible Duplicate: XAML Code for TREEVIEW
This question already has an answer here: Closed 10 years ago.

Possible Duplicate:

XAML Code for TREEVIEW

Following is the code for TreeView in XAML:

<TreeView Height="338" HorizontalAlignment="Left" Margin="6,6,0,0"
          Name="DisksTreeView1" VerticalAlignment=开发者_如何学C"Top" Width="293">
    <TreeView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding ImagePath}" Width="32" Height="32"/>
                <TextBlock Text="{Binding Namee}"/>
            </StackPanel>
        </DataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

And following is the code behind:

Dim adpt As New DatabaseDearDataSetTableAdapters.DiskListTableAdapter
For Each k As KeyValuePair(Of String, GenreSet) In GenreSetDictionary
    Dim t As New TreeNodeSet
    t.Genre = True
    t.Imagepath = k.Value.IconPath
    t.Namee = k.Key
    Dim pnode As New TreeViewItem
    pnode.DataContext = t
    For Each z As DatabaseDearDataSet.DiskListRow In adpt.GetDataByGenre(t.Namee)
        Dim tt As New TreeNodeSet
        tt.Genre = False
        tt.Imagepath = IconDictionary(z.DiskIcon).IconPath
        tt.Namee = z.DiskName
        Dim cnode As New TreeViewItem
        cnode.DataContext = tt
        pnode.Items.Add(cnode)
    Next
    DisksTreeView1.Items.Add(pnode)
Next

I am able to get the pnode added to the TreeView and all the node breakpoints are hit. The problem is that in the TreeView, I am only able to see the parent view arrow, the rest of the name, image, etc. are not getting displayed. It is like this:

TreeView nodes not being displayed [duplicate]

How can I fix this?


In your code-behind you are creating a TreeViewItem instances and adding them to the TreeView. But what you need to do - is to define your model classes in a hierarchical way, bind top level items collection to the ItemsSource property of the TreeView, and use HierarchicalDataTemplate instead of the DataTemplate to display nested items.


The answer for the above issue is displayed at the following page: XAML and Required Code Set

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号