A: 

Might be worth having a look at this post by Marlon Grech.

serialhobbyist
I couldnt understand a thing in the post you referred to....can you please refer me to a simpler post....something for an amateur that is???? Please!!!!
Gagan
+3  A: 

The good news is that you're doing a lot more work here than you need to, which is probably why you're having trouble.

The bad news is that you should really study a little more about WPF to properly grok this and come up with a good approach that's clean and concise. I'll try and point you in the right direction.

Firstly, you should get your head around ItemsControl. It's a really powerful class and is the base class of many of the everyday controls you would use in a WPF application. You should understand how binding any collection (IEnumerable, IList, IBindingList etc) to the ItemsSource property of ItemsControl will cause child items to be created.

You should then understand (if you don't already) how data types are converted into UI elements via DataTemplates. This is a simple but powerful concept.

Then you should experiment with a small extension to the above, namely HeaderedItemsControl and HierarchicalDataTemplate. This will give you all the tools you need to use the TreeView in the way you want to.

At no point would you need to create any TreeViewItems in C# code. If you can get the underlying data objects to reflect the hierarchy you want to display (irrespective of whether each node is a simple text label or a data grid) then you can create hierarchical data templates for all levels and have WPF take care of binding everything and creating the TreeViewItems for you.

EDIT

I have some questions for your edited question:

  1. What is the difference between Root and Nodes?
  2. Do you have a class hierarchy that models the relationship between nodes? If so, just use that rather than copying the objects into instances of Root and Nodes. I'll give you a made up example.

Let's assume you have Customers who place Orders, and each order has Items.

public class Customer
{
    public string Name { get; set; }
    public IEnumerable<Order> Orders { get; set; }
}

public class Order
{
    public DateTime PurchaseDate { get; set; }
    public IEnumerable<OrderItem> Items { get; set; }
}

public class OrderItem
{
    public string ProductName { get; set; }
    public int Quantity { get; set; }
    public double UnitPrice { get; set; }
    public double TotalPrice { get; set; }
}

The above types represent a hierarchy. If you have a structure like this, then you can bind it directly to the UI. You don't need to create any Root or Node objects. This is the WPF way :)

(Note that if you don't have the above class hierarchy, you might set about creating one specifically for use in the UI. Read more about the MVVM pattern if you're interested.)

In your XAML you would define the TreeView as:

<TreeView x:Name="_treeView" ItemsSource="{Binding}">
  <TreeView.Resources>
    <HierarchicalDataTemplate DataType="{x:Type data:Customer}"
                              ItemsSource="{Binding Path=Orders}">
      <TextBlock Text="{Binding Name}"/>
    </HierarchicalDataTemplate>
    <DataTemplate DataType="{x:Type data:Order}">
      <StackPanel>
        <TextBlock Text="{Binding PurchaseDate}"/>
        <ListView ItemsSource="{Binding Items}">
          <ListView.View>
            <GridView>
              <GridViewColumn DisplayMemberBinding="{Binding ProductName}" />
              <GridViewColumn DisplayMemberBinding="{Binding Quantity}" />
              <GridViewColumn DisplayMemberBinding="{Binding UnitPrice}" />
              <GridViewColumn DisplayMemberBinding="{Binding TotalPrice}" />
            </GridView>
          </ListView.View>
        </ListView>
      </StackPanel>
    </DataTemplate>
  </TreeView.Resources>
</TreeView>

And in code-behind, you'd do something like this:

 _treeView.DataContext = customers; // eg. IEnumerable<Customer>
Drew Noakes
Hey Drew, thank you for a yet another simple and easily understandable answer. After going through what you wrote, I remodeled my code, and now, I am not defining any treeviewitem in the C# code. But what I am still unable to get is how to get a gridview to become a childnode of the treeview. Can you please help me with that. I am editing the code in my question above to reflect the changes that I have made. Please reply soon. I am counting on you....really!
Gagan
Hi Gagan. Looks like you've made some good progress. It's pretty late here so I'll have a look at this in the morning.
Drew Noakes
Hey Drew, your comment gives me confidence and hope! I realy am counting on you. Also, I wanted to tell you, that I was doing a little bit of research, and found that there is a class called DataGrid in the namespace Microsoft.Windows.Controls . So, what I did was to make an object of this class(DataGrid obj_dgv=new DataGrid), and that bind it with a datatable(obj_dgv.ItemsSource=dt.DefaultView). And then make this object a child node of the tree(obj_tree.RootList.Add(obj_dgv)). But this retured an error....so I am again stuck with nothing. Please help!!!!
Gagan
Hey Drew, thank you for all this excellent explanation. I tried whatever you told me, and it works the way it should. However, it was still very complex for me (I am very new to all this, I hope you know). So, I was playing with the little I know, and something striked me. I just did this: DataGrid dgobj=new DataGrid();dgobj=DataTableObj.DefaultView;treeobj.Items.Add(dgobj);and Bam! I got the datagrid as a child of the tree!!!! :) :)
Gagan