tags:

views:

84

answers:

3

I want to bind nested collection to grid panel but not sure how dynamically I can do this.

Here is my collection:

Public class GrandParent
{
    string name;
    Icollection<Parent> ParentCollection;
}

Public class Parent
{
    string lastname;
    Icollection<Child> ChildCollection;
}

Public class Child
{
    string name;
    int age;
}

I want output like this:

GrandParent.Name Parent1.LastName Parent2.LastName Parent3.LastName

GP1 Children[GP1,Parent1.LastName] Children[GP1,Parent2.LastName] Children[GP1,Parent3.LastName]
GP2 Children[GP2,Parent1.LastName] Children[GP2,Parent2.LastName] Children[GP2,Parent3.LastName]

Where Parent.LastNames are static.

As of now, my viewmodel class (binding class) is like this:

Class viewmodel
{
    collection<string> GrandParentNames
    {
    }

    collection<string> Parent1LastNames
    {
    }

    collection<string> Parent2LastNames
    {
    }

    collection<string> Parent3LastName
    {
    }
}

Could anyone please suggest me better approach with this?

+1  A: 

Your question is a little vague; I don't understand exactly what you are trying to bind to... What do you mean by:

bind nested collection to grid panel

I can tell you right away though, that when using nested collections of data, your best bet is always going to be using a HierarchicalDataTemplate. Here are a couple examples of using one.

Charlie
A: 

Hey Charlie, looks like I didn't put the my question well.... Sorry for the same..

My collections are nested but my output I want is not nested..

it should be like...

Parent Property I want to use for header name GranParent Name I want o use for First Column while Child properties I want to use for Other Columns

So Input is Grant Parent -> Parent -> Child (1 to many)

out put is

             --         Parent        --               Parent1

GrandParent.Name -- Child of Parent -- Child of Parent1 GrandParent1.Name -- Child of Parent -- Child of Parent1

IF I take an example here...

Country -> Designation -> Person (Name, Age)

e.g. US - Manager - Person(A, 26) US - Chief ARchitect - Person (B, 28) UK - Manager - Person(C, 26) UK - Chief Architect - Person (D, 28)

**out put is :**

(Header)

Country --- Mangaer --- Chief Architect

(R0w1)


US --- A, 26 --- B, 28

(R0w2)

UK --- C,26 --- D, 28

would apprecitate for any of clue or code...

A: 

I'm just going to make the assumption based of your name that your are doing this in wpf.

In your code, you had the right idea for the Model, but (assuming i understand what your aiming for) you went a little astray on the ViewModel. Because the GrandParent class has a collection of Parents, and the Parents a collection of Children, in your ViewModel you'll only need a collection of GrandParrents in your ViewModel. so your ViewModel will be closer to:

class ViewModel
    {
        ICollection<GrandParent> Grents;
    }


as goes your View, the easiest way I know of is through data templates. You'll have to use a list box in your grid, but you can format the list box how ever you want to get the result your looking for, this is the glory of WPF. anyway, i made a simple example here:

<Window.Resources>

 <local:ViewModel x:Key="ViewModelDataSource" d:IsDataSource="True"/>
 <DataTemplate x:Key="GrandParents_Data_Template">
  <StackPanel>
<TextBlock Text="{Binding name}"/>
<ListBox ItemsSource="{Binding ParentCollection}" ItemTemplate="{DynamicResource Parents_Data_Template}"/>

</Window.Resources>

Hope that helps get you going in the right direction at least. Let me know if you have any more questions.

Narcolapser