title: LINQ to SQL as databinding source for WPF Treeview
body: <p>I wonder if someone could provide a simple example of the following. (preferably in VB.Net):</p>
  
  <p>I have an SQL database with related tables and I am successfully using LINQ to SQL in other areas of my application, but am a little stuck with the Heirarchical Treeview. </p>
  
  <p>I would simply like to use a LINQ query from the database as a source for a WPF Treeview. If I can set the ItemsSource for the treeview as my LINQ result and just set the databinding for treeview items to the various columns that would make my day, but I cant seem to get it cooking.</p>
  
  <p>After spending hours searching the net, I can't find many examples that show this very simply at all. I have found similar ideas but nothing simple and specific for a newbie like myself.</p>
  
  <p>As far as I understand, the relationships defined in the DBML file should stay intact when executing the LINQ query. So, can I have something like this as the ItemsSource for the Treeview?</p>
  
  <pre><code>Dim pdc As New ProjectDataContext()  
  Public Property Selection() As Integer
  
  Dim tree = From c In pdc.Customers _
             Where c.CustomerID = _Selection _
             Select c
  
  projecttreeview.ItemsSource = tree
  </code></pre>
  
  <p>Then, the databinding for the TreeView Items could just be {Binding CustomerName) for a parent node and say {Binding Orders.OrderName} as a child node. eg:</p>
  
  <pre><code>&lt;TreeView Name="projecttreeview"&gt;
    &lt;TreeView.ItemTemplate&gt;
      &lt;HierarchicalDataTemplate ItemsSource="{Binding Customers}"&gt;
        &lt;TextBlock.Text="Binding CustomerName}"/&gt;
          &lt;HierarchicalDataTemplate.ItemTemplate&gt;
            &lt;DataTemplate&gt;
              &lt;TextBlock.Text="{Binding Orders.OrderName}"/&gt;
            &lt;/DataTemplate&gt;
          &lt;/HierarchicalDataTemplate.ItemTemplate&gt;
        &lt;/HierarchicalDataTemplate&gt;
      &lt;/TreeView.ItemTemplate&gt;
  &lt;/TreeView&gt;
  </code></pre>
  
  <p>Obviously this is not quite working out as simply as I would like. Any pointers would be greatly appreciated.</p>