Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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:
C#
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:
C#
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:
XML
<TreeView.ItemTemplate>
  <HierarchicalDataTemplate ItemsSource="{Binding Children}">
    <TextBlock Text="{Binding Name}" />
  </HierarchicalDataTemplate>
</TreeView.ItemTemplate>
Posted
Updated 19-Sep-17 3:16am
v2

This shouldnt be so hard. All you need to do is store all the people in the database that could look something like this:
VB
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 :)
 
Share this answer
 
Comments
rob.evans 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.
You should probably have a look at ObservableCollections and CollectionViewSources.
I'll see what I can find on the subject that may help you, but in the mean time have a search for your self.
Try have a look at what this guy did.
Tuning Up The TreeView - Part 1[^]

Good luck :)
 
Share this answer
 
v2
Comments
Graeme_Grant 19-Sep-17 9:41am    
WHY are you answering a 5-YEAR-OLD question that has ALREADY ACCEPTED an answer. Please don't, instead focus on current question ONLY - there are always plenty waiting for help.
Erik Rude 19-Sep-17 11:01am    
Sorry my bad I wasn't paying attention to the date. I was looking at the unanswered questions only, then I changed the filtering and I didn't notice the existing answers. If this doesn't answer your question I'm sorry to have upset you this much. Anyway I can't see that there is an accepted answer. Hope your day improves :)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900