Click here to Skip to main content
Rate this: bad
good
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",
                        Children=
                        { 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:
      <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Children}">
          <TextBlock Text="{Binding Name}" />
        </HierarchicalDataTemplate>
      </TreeView.ItemTemplate>
Posted 26-Jul-12 4:41am
Edited 26-Jul-12 4:43am
v2

1 solution

Rate this: bad
good
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
etc..
 
Now you could easely retrive all the data you need to make the connections.
 
So good luck to you Smile | :)
  Permalink  
Comments
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 CPallini 375
1 Sergey Alexandrovich Kryukov 370
2 BillWoodruff 314
3 George Jonsson 274
4 OriginalGriff 257
0 OriginalGriff 5,030
1 CPallini 4,115
2 Sergey Alexandrovich Kryukov 3,554
3 George Jonsson 2,826
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 31 Jul 2012
Copyright © CodeProject, 1999-2014
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