I've been searching for a some working code during the last day or two to setup a treeview during my WPF application to show some database data. I personally use VB like me a lot more positive about it - which drastically limited the code good examples available. More good examples i discovered used by hand joined data to populate each node versus a database that was little help. I finally found some code which bound the each node to some query and related the query using a DataRelation. Great this will work perfect!

The example used the Northwind database and so i transformed it to my database and blocked within the queries. To my shock the treeview populated fine except.... all of the information is invisible.

This is actually the code:

Imports MySql.Data.MySqlClient
Imports System.Data
Imports System.ComponentModel
Imports System.Xml
Imports System.IO
Class Window2
Dim connStr As String = "Server=127.0.0.1;Database=psdb;Uid=root;Pwd=;Connect    Timeout=30;"
Dim conn As New MySqlConnection(connStr)
Function GetRelationalData() As DataSet

    Dim CategoryAdapter As MySqlDataAdapter = New MySqlDataAdapter("select distinct Dist_name, dist_id from distributors".ToString, conn)
    Dim ProductsAdapter  As MySqlDataAdapter = New MySqlDataAdapter("select d.dist_id, t.Title_name, title_id from titles t, distributors d where d.dist_id = t.dist_id".ToString, conn)
    Dim ProductData As DataSet = New DataSet()
    CategoryAdapter.Fill(ProductData, "Categories") 'fill Categories
    ProductsAdapter.Fill(ProductData, "Products") 'fill products
    Dim CategoryRelation As DataRelation
    CategoryRelation = New DataRelation("ChildrenRelationship", _
    ProductData.Tables("Categories").Columns("dist_id"), _
    ProductData.Tables("Products").Columns("dist_id"), True)
    CategoryRelation.Nested = True
    ProductData.Relations.Add(CategoryRelation)
    Return ProductData
End Function
Private Sub Button1_Click(ByVal sender As System.Object, _
                          ByVal e As RoutedEventArgs) Handles Button1.Click
    BindData()
End Sub
Private Sub BindData()
    TreeView1.DataContext = GetRelationalData()
End Sub

End Class

and also the XAML:

<Window x:Class="Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="400" Width="550"><Window.Resources>
<DataTemplate x:Key="ProductTemplate" >
    <TextBlock Text="{Binding ProductName}"></TextBlock>
</DataTemplate>
<HierarchicalDataTemplate x:Key="CategoryTemplate" 
                  ItemsSource="{Binding ChildrenRelationship}" 
                  ItemTemplate="{StaticResource ProductTemplate}">
    <TextBlock Text="{Binding CategoryName}"></TextBlock>
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>

and here things i finish track of the information appears to become there however, you can't view it. Help!

enter image description here

PS there might be some confusion regarding the objects. I returned attempted to help keep as near to the original code as you possibly can after i discovered the issue only changing the queries with my very own - the initial code used "Groups" and "Items" while I "use Marketers" and "Game titles"

I'd recommend getting Snoop and running it while the application is running to determine what your UI Elements are really certain to.

I suspect it's just a problem of getting an invalid DataBinding inside your TextBlock

OK Tim does not want the Kudos I suppose. This is actually the remedied code:

<Window x:Class="Window2" 
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="Window1" Height="400" Width="550">
<Window.Resources> 
<DataTemplate x:Key="ProductTemplate" > 
<TextBlock Text="{Binding title_name}">
</TextBlock> 
</DataTemplate> 
<HierarchicalDataTemplate x:Key="CategoryTemplate"           
ItemsSource="{Binding ChildrenRelationship}"                    
ItemTemplate="{StaticResource   ProductTemplate}">     
<TextBlock Text="{Binding dist_name}"></TextBlock> 
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>