How do I merge 2 Observable collections in to a WPF Treeview in a Parent / Child relationship using vb.net

Posted on 2011-10-15
Medium Priority
Last Modified: 2013-11-12
Hello everyone,

I'm quite new to WPF and even newer to EE, so please go easy on me.  I have two observable collections, the first is a nParent and the second is nChild.  nParent has only a single "Name" property and nChild has 2 properties, "Name" and "CRF".  Name is string for both and CRF is an integer.  

I can build up the two collections, but the problem I'm having is associating the two classes (meaning each child should be assigned to each parent) and then binding the collection to a treeview.  

I tried following the example here: http://www.experts-exchange.com/Microsoft/Development/Microsoft_Programming/WPF_and_Silverlight/A_3685-Tree-View-Data-Binding-n-level.html?sfQueryTermInfo=1+30+treeview+wpf  but it still doesn't seem to click.

If someone wouldn't mind helping, I'd greatly appreciate it.  Additionally, please let me know if you need any additional information.

For what it's worth, here's my code so far:


<Window x:Class="MainWindow"
    Title="MainWindow" Height="353" Width="669">
            <TextBox Name="Add_Item" DockPanel.Dock="top"/>
            <Button Name="Add_Parent" Content="Add Parent Node" DockPanel.Dock="Top"/>
            <Button Name="Add_Child" Content="Add Child Node" DockPanel.Dock="Top"/>
            <TreeView Name="Tree1" DockPanel.Dock="Left" Width="147" ItemsSource="{Binding}" AllowDrop="True">
                    <HierarchicalDataTemplate ItemsSource="{Binding Path=NodeName}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Path=NodeName}"/>
            <DataGrid Name="DataGrid1" DockPanel.Dock="Left" Width="206" ItemsSource="{Binding}"/>
            <DataGrid Name="DataGrid2" DockPanel.Dock="Left" Width="194" AllowDrop="True" />

Open in new window

Main Window:
Imports System.Collections.ObjectModel

Class MainWindow
    Private _Parent_Nodes As New ObservableCollection(Of Parent_Nodes)
    Private _Child_Nodes As New ObservableCollection(Of Child_Nodes)
    Public ReadOnly Property ParentCollection() As ObservableCollection(Of Parent_Nodes)
            Return _Parent_Nodes
        End Get
    End Property
    Public ReadOnly Property ChildCollection() As ObservableCollection(Of Child_Nodes)
            Return _Child_Nodes
        End Get
    End Property

    Private Sub Add_Parent_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Add_Parent.Click
        _Parent_Nodes.Add(New Parent_Nodes(Add_Item.Text))
        DataGrid1.ItemsSource = ParentCollection
    End Sub
    Private Sub Add_Child_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Add_Child.Click
        _Child_Nodes.Add(New Child_Nodes(Add_Item.Text))
        DataGrid2.ItemsSource = ChildCollection
    End Sub
    Public Sub New()

        ' This call is required by the designer.

        ' Add any initialization after the InitializeComponent() call.
        Tree1.ItemsSource = ParentCollection
    End Sub

End Class

Open in new window

Public Class Parent_Nodes
    Private _sName As String
    Private _p1 As String

    Sub New(ByVal p1 As String)
        _p1 = p1
    End Sub

    Public ReadOnly Property NodeName() As String
            Return _p1
        End Get
    End Property
End Class

Public Class Child_Nodes
    Private _sName As String
    Private _c1 As String

    Sub New(ByVal c1 As String)
        _c1 = c1
    End Sub

    Public ReadOnly Property NodeName() As String
            Return _c1
        End Get
    End Property
End Class

Open in new window

Any help is appreciated!
Question by:sous2817
LVL 83

Accepted Solution

CodeCruiser earned 2000 total points
ID: 36976645

Author Closing Comment

ID: 36976679
Thanks for the links!  I spent quite a bit of time searching and I guess I just couldn't seem to get the right combination of key words.

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Loops Section Overview

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question