Click here to Skip to main content
12,749,392 members (32,034 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# WPF Treeview MVVM
I have used Josh Smith's article, "Simplifying the WPF TreeView by Using the ViewModel Pattern", to adapt his technique to a WPF, MVVM, Prism, Unity type application. It works great when I use his sample "hardcoded" data source provided in his download called "TreeVeiwWithViewModelDemo", but I am stumped on how to modify the code in order to recursively add data to the Person object from a SQL Server database. I am hoping that somebody has already done that and can give me a push in the right direction.

Josh's code is as follows:
public static Person GetFamilyTree()
            // In a real app this method would access a database.
            return new Person
                Name = "David Weatherbeam",
                Children =
                    new Person
                        Name="Alberto Weatherbeam",
                        { etc...

I need to modify this to access a database and return the list Person which is defined as follows:
public class Person
        readonly List<Person> _children = new List<Person>();
        public IList<Person> Children
            get { return _children; }

        public string Name { get; set; }

... and the WPF item template looks like this:
        <HierarchicalDataTemplate ItemsSource="{Binding Children}">
          <TextBlock Text="{Binding Name}" />
Posted 26-Jul-12 5:41am
Updated 26-Jul-12 5:43am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This shouldnt be so hard. All you need to do is store all the people in the database that could look something like this:
UniquePersonID  PersonNAme              Level              Parent
1               David Weatherbeam       0                  Nothing/Null
2               Alberto Weatherbeam     1                  1


Now you could easely retrive all the data you need to make the connections.

So good luck to you :)
revans722 2-Aug-12 8:30am
Hmmm. If it wasn't so hard (for me only, maybe), then I would not have asked the question. I already have the people in a hierarchical database. I was hoping for an answer with some actual code with recursion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170215.1 | Last Updated 31 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100