Click here to Skip to main content
11,580,926 members (66,326 online)
Rate this: bad
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 4:41am
Edited 26-Jul-12 4: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 Smile | :)
revans722 at 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
0 Frankie-C 195
1 OriginalGriff 184
2 F-ES Sitecore 80
3 Andy Lanng 75
4 Afzaal Ahmad Zeeshan 75
0 OriginalGriff 1,108
1 Sergey Alexandrovich Kryukov 785
2 Abhinav S 622
3 F-ES Sitecore 500
4 Suvendu Shekhar Giri 433

Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 31 Jul 2012
Copyright © CodeProject, 1999-2015
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