0

I want to populate a tree getting parent and nodes from database in WPF. My database structure is as; enter image description here

Here deficiencyID is the id of the node and parentID is the id of the parent.

michele
  • 2,091
  • 1
  • 16
  • 24
c-sharp
  • 573
  • 1
  • 9
  • 25
  • Take a look at this article: [Simplifying the WPF TreeView by Using the ViewModel Pattern](http://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode) from WPF guru Josh Smith. – Lubo Nov 23 '12 at 13:32
  • I am not following this article. – c-sharp Nov 23 '12 at 15:05

1 Answers1

0

You can model your db table with an entity like this:

public class Deficiency
{
    public int DeficiencyID { get; set; }
    public int ParentID { get; set; }
    //... OTHER PROPERTIES ...//
}

then take a look at this answer.

EDIT: Your XAML with the treeview can be like this:

<!--Bind to Groups generated from codebehind 
    Every group have property Name and Items -->
<TreeView Name="treeview1" ItemsSource="{Binding Groups}" >
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Path=Items}">
            <!-- Here Bind to the name of the group => this is the Parent ID -->
            <TextBlock Text="{Binding Path=Name}" />
            <HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                    <!-- Here the data context is your class Deficiency, 
                         so bind to its properties-->
                        <TextBlock Text="{Binding Path=DeficiencyID}"/>
                        <!-- ... -->
                        <TextBlock Text="{Binding Path=OtherProperties}"/>
                        <!-- ... -->
                    </StackPanel>
                </DataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

and your codebehind should be something like this:

List<Deficiency> myList = new List<Deficiency>();
// here load from DB //       
ICollectionView view = CollectionViewSource.GetDefaultView(myList);
view.GroupDescriptions.Add(new PropertyGroupDescription("ParentID"));
treeview1.DataContext = view;

HTH

Community
  • 1
  • 1
michele
  • 2,091
  • 1
  • 16
  • 24
  • Michele, I don't get your point fully. Could you provide the code in more elaborative form. because I have already this class, but I am wondering how to force XAML to show the generic tree view. Which has some deeper nodes as well. – c-sharp Nov 23 '12 at 13:25
  • Michele --Here the Items means{Binding Path=Items}? Is this the list name or what? THanks – c-sharp Nov 23 '12 at 14:17
  • The ICollectionView has a property called Groups that you populate adding GroupDescription. Every Group has two main properties: Name (that you bind at the first level of your HierarchicalDataTemplate) and Items (that is the list of the grouped object). So Items is the property of the Group that I bind to the ItemsSource property of the HierarchicalDataTemplate. Reference here http://msdn.microsoft.com/en-us/library/system.windows.data.collectionview.aspx – michele Nov 23 '12 at 14:28
  • Thank you Michele , it works, but there is a small problem. most upper root is not showing. But its childs and sub childs are also displaying. – c-sharp Nov 23 '12 at 14:44
  • How can we show several roots, right now my function which separates the roots and childs in the generic list from dataset, using a single root. – c-sharp Nov 23 '12 at 14:53