Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4619840
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:28:44+00:00 2026-05-22T02:28:44+00:00

Following is the code I wrote for generating treeview hierarchy, For Each k As

  • 0

Following is the code I wrote for generating treeview hierarchy,

    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
        pnode.Visibility = True
        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

Following is the code I have used in XAML:

<TreeView Height="211" HorizontalAlignment="Left" Margin="19,15,0,0" Name="TreeView1" VerticalAlignment="Top" Width="346">
    <TreeView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding ImagePath}" Width="32" Height="32"/>
                <TextBlock Text="{Binding Namee}" VerticalAlignment="Center" HorizontalAlignment="Center" />
            </StackPanel>
        </DataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

However, I was not able to get that work, could you please tell me where my XAML went wrong please.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:28:45+00:00Added an answer on May 22, 2026 at 2:28 am

    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 <HierarchicalDataTemplate ItemsSource="{Binding Disks}"
    • 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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I needed some simple string encryption, so I wrote the following code (with a
In C++Builder, I wrote the following code (in Button1Click handler), When I run in
I've got the following bit of code (that another developer wrote) that I need
I have the following code: MemoryStream foo(){ MemoryStream ms = new MemoryStream(); // write
I'm generating a JavaScript alert with following code in C# .NET page: Response.Write(<script language=JavaScript>
I am trying to use the following code to write data into an excel
I am a AS3 novice learning PureMVC and want to write code following best
How can I write the following code quickly in emacs? \newcommand{\cA}{\mathcal A} \newcommand{\cB}{\mathcal B}
I'm trying to write a piece of code that will do the following: Take
Following code, when compiled and run with g++, prints '1' twice, whereas I expect

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.