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

Posted on 2011-10-15
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:  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

    LVL 1

    Author Closing Comment

    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

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    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…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    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.…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now