Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
110
Binding to Parent/Child in a Database
posted

I am trying to display a simple parent/child treeview by reading the parent and child from a database. Do you have an example of reading from a database (i.e Northwind db Customer/Orders) and displaying that in a XamTree? 

The data would come back from the database as

  • customer1   Order1
  • customer1   Order2
  • customer2   Order3
  • customer2   Order4

From other examples I have seen it seems I need to take that data and reformat it before binding to the xamtree. I have spent all day on this so an example would be great.

Thanks, Ken

 

 

Parents
  • 610
    posted

    Sorry, I mean to give a sample and clicked post....

    Inside the tree give it a template for the Heirarchy:

     <ig:HierarchicalDataTemplate ItemsSource="{Binding SubFolders}" Template="{StaticResource RootFolder}">
                            <ig:HierarchicalDataTemplate.HierarchicalItemTemplate>
                                <ig:HierarchicalDataTemplate ItemsSource="{Binding SubFolders}" Template="{StaticResource SubFolder1}"/>                               
                            </ig:HierarchicalDataTemplate.HierarchicalItemTemplate>
                        </ig:HierarchicalDataTemplate>
                    </ig:XamTree.HierarchicalItemTemplate>

    The tree gets it's item source set to the parent collection, then the child collections are mapped as above. My children are called SubFolders. Then just set the template to a template like this in the Resources section of teh Xaml:

     <DataTemplate x:Key="SubFolder1">
                    <StackPanel Orientation="Horizontal">
                        <Image Margin="0,0,5,0"
              Height="16"
              Width="16"
              Source="/ManagementModule;component/Images/folder16.png"/>
                        <TextBlock Text="{Binding Description}" Foreground="Black"/>
                    </StackPanel>
                </DataTemplate> 

    Works like a champ! You should be able to do this with any data source if it's parent child relationship is setup correctly.

    -Jeff

Reply Children
No Data