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

  • SEARCH
  • Home
  • 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 7645649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:59:36+00:00 2026-05-31T09:59:36+00:00

I have MyCollection of MyPOCO object (that has two string properties). When I try

  • 0

I have MyCollection of MyPOCO object (that has two string properties).

When I try to implement a HierarchicalDataTemplate into a treeview the binding is not working, I get the class name.

I know that if i take out the datatemplate from the control everything works fine but i am interested to see why this example is not working.

<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource testVM}}">
    <sdk:TreeView Margin="8,8,8,111" ItemsSource="{Binding MyCollection}">
        <sdk:HierarchicalDataTemplate ItemsSource="{Binding MyPOCO}">
            <sdk:HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Property1}"/>
                        <TextBlock Text="{Binding Property2}"/>
                    </StackPanel>
                </DataTemplate>
            </sdk:HierarchicalDataTemplate.ItemTemplate>
        </sdk:HierarchicalDataTemplate>
    </sdk:TreeView>
</Grid>

Here is the ViewModel also.

namespace MyPOCProject

{
public class MyPOCO
{
private string property1;

    public string Property1
    {
        get { return property1; }
        set { property1 = value; }
    }

    private string property2;

    public string Property2
    {
        get { return property2; }
        set { property2 = value; }
    }

    public MyPOCO(string p1, string p2)
    {
        property1 = p1;
        property2 = p2;
    }
}

public class MyViewModel : INotifyPropertyChanged
{
    private ObservableCollection<MyPOCO> myCollection;

    public ObservableCollection<MyPOCO> MyCollection
    {
        get { return myCollection; }
        set { myCollection = value; RaisePropertyChanged("MyCollection"); }
    }

    public MyViewModel()
    {
        MyPOCO _poco1 = new MyPOCO("aaa1", "bbb1");
        MyPOCO _poco2 = new MyPOCO("aaa2", "bbb2");
        MyPOCO _poco3 = new MyPOCO("aaa3", "bbb3");

        MyCollection = new ObservableCollection<MyPOCO>() { _poco1, _poco2, _poco3 };
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

}

So what am I doing wrong?

AGAIN … I am interested in this particular example. I want to know what’s wrong with this example and why.

Thanks.

  • 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-31T09:59:37+00:00Added an answer on May 31, 2026 at 9:59 am

    The code you posted is not hierarchical, In other words: The MyPOCO Class is not containing a property MyCollection<MYPOCO> Children.
    Here is an example for the HierarchicalDataTemplate

    Xaml:

    <sdk:TreeView x:Name="MyTreeView"
                  HorizontalAlignment="Left"
                  ItemsSource="{Binding MyCollection}"
                  Width="200" Height="280">
        <sdk:TreeView.ItemTemplate>
            <sdk:HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">
                <TextBlock Text="{Binding Path=Header}"/>
                <sdk:HierarchicalDataTemplate.ItemTemplate>
                    <sdk:HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">
                        <TextBlock Text="{Binding Path=Header}"/>
                    </sdk:HierarchicalDataTemplate>
                </sdk:HierarchicalDataTemplate.ItemTemplate>
            </sdk:HierarchicalDataTemplate>
        </sdk:TreeView.ItemTemplate>
    </sdk:TreeView>
    

    Codebehind classes:

    public class MyPoco
    {
        public MyPoco(string header, int sampleChildrenCount)
        {
            this.Header = header;
            this.Children = new ObservableCollection<MyPoco>();
            if (sampleChildrenCount > 0)
            {
                for (int i = 0; i < sampleChildrenCount; i++)
                {
                    string newHeader = String.Format("Test {0}", sampleChildrenCount * i);
                    var myPoco = new MyPoco(newHeader, sampleChildrenCount - 1)
                    this.Children.Add(myPoco);
                }
            }
        }
        public string Header { get; set; }
        public ObservableCollection<MyPoco> Children { get; set; }
    }
    
    public class MyViewModel
    {
        public MyViewModel()
        {
            MyCollection = new ObservableCollection<MyPoco>();
            for (int i = 0; i < 6; i++)
            {
                this.MyCollection.Add(new MyPoco(String.Format("Test {0}", i), 5));
            }
        }
        public ObservableCollection<MyPoco> MyCollection { get; set; }
    }
    

    Codebehind startup:

    public MainPage()
    {
        public MainPage()
        {
            InitializeComponent();
            MyTreeView.DataContext = new MyViewModel();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ObservableCollection<Object> that holds two different types of objects: Directory and File
I have an ObservableCollection which is dataContext for my treeview when I try to
I have a generic collection MyCollection<T> that I've made, and everything works fine except
I have the following string: $/Mycollection/Branches/Dev/New/php/MySite/src/MySite/somefolder/src/sad.php I need to create regex pattern and take
I have a control mycontrol.ascx This control has a property: private GenericCollection<Item> myCollection; public
I have class myCollection that inherit from Dictionary. Want to hide the add method
I have a List<object[]> MyCollection which is a result of a SELECT SQL query.
I have a list of objects and each object has a ExpirationDate property which
I have an array: string[] exceptions = new string[] { one, two, one_1, three
I have a TreeView which uses a HierarchicalDataTemplate to bind its data. It looks

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.