locked
How to bind data to a gridview which is inside another gridview?

    Question

  • Hi All,

    I have a gridview whose template consist of another Gridview and a ListView .

    Now I wrote a class which has 2 lists its properties. I want list-1 to be binded to the ListView and list-2 to the GridView.

    How can I bind that class object to the parent GridView, So that the lists inside that object shall be used by the child ListView and GridView.??

    Any help is greatly in need.

    Thanks

    Rajesh

    Thursday, July 31, 2014 10:16 AM

Answers

  • If you have a list that contains lists, the inner listview can bind to the inner list.  Like this:

    class GroupObject

    {
    private string _groupname = "";
    private List<string> _innerList = new List<string>();

    public string GroupName {
        get
    {
        return _groupname;
    }

    set;}         
    public List<string> InnerList {
    get
    {
        return _innerList;
    }
    ; set;}

    public GroupObject  (string groupname);
    {
         GroupName = groupname;
    }

    }

    List<GroupObject> OuterList = new List<GroupObject>();

    GroupObject Animals = new GroupObject("Animals");

    Animals.InnerList.add ("Dog");

    Animals.InnerList.add ("Cat");

    Animals.InnerList.add ("Bear");

    Animals.InnerList.add ("Lion");

    OuterList.add(Animals);

    GroupObject Letters= new GroupObject("Letters");
    Letters.InnerList.add ("A");

    Letters.InnerList.add ("B");

    Letters.InnerList.add ("C");

    Letters.InnerList.add ("D");

    Outerlist.add(Letters);

    <ListView x:Name="MyListView">

      <ItemTemplate>

        <DataTemplate>
          <StackPanel>

            <TextBlock Text="{Binding GroupName}"/>
            <ListView  ItemsSource = "{Binding InnerList}">

              <ItemTemplate>

                <DataTemplate>

                   <TextBlock Text="{Binding}"/>

                </DataTemplate>

              </ItemTemplate>

            </ListView>

          <StackPanel>

        </DataTemplate>

      </ItemTemplate>

    </ListView>


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, July 31, 2014 11:43 AM
    Moderator
  • Hi.

    <GridView ItemsSource={Binding RootItems}>
     <GridView.ItemTemplate>
      <DataTemplate>
         <GridView ItemsSource={Binding FirstCollection}>
            <GridView.ItemTemplate>
               <DataTemplate>
                   <TextBlock Text={Binding Title}/>
               </DataTemplate>
            <GridView.ItemTemplate>
         </GridView>
         <ListView ItemsSource={Binding SecondCollection}>
            <ListView.ItemTemplate>
               <DataTemplate>
                  <TextBlock Text={Binding name}/>
               </DataTemplate>
            <ListView.ItemTemplate>
         </GridView>
      </DataTemplate>
     <GridView.ItemTemplate>
    </GridView>
    
    
    

    //models:
    public class RootItem
    {
        public List<A> FirstCollection {get;set;}
        public List<B> SecondCollection {get;set;}
    }
    
    public class A
    {
        public string Title {get;set;}
    }
    public class B
    {
        public string Name {get;set;}
    }




    Thursday, July 31, 2014 11:44 AM

All replies

  • If you have a list that contains lists, the inner listview can bind to the inner list.  Like this:

    class GroupObject

    {
    private string _groupname = "";
    private List<string> _innerList = new List<string>();

    public string GroupName {
        get
    {
        return _groupname;
    }

    set;}         
    public List<string> InnerList {
    get
    {
        return _innerList;
    }
    ; set;}

    public GroupObject  (string groupname);
    {
         GroupName = groupname;
    }

    }

    List<GroupObject> OuterList = new List<GroupObject>();

    GroupObject Animals = new GroupObject("Animals");

    Animals.InnerList.add ("Dog");

    Animals.InnerList.add ("Cat");

    Animals.InnerList.add ("Bear");

    Animals.InnerList.add ("Lion");

    OuterList.add(Animals);

    GroupObject Letters= new GroupObject("Letters");
    Letters.InnerList.add ("A");

    Letters.InnerList.add ("B");

    Letters.InnerList.add ("C");

    Letters.InnerList.add ("D");

    Outerlist.add(Letters);

    <ListView x:Name="MyListView">

      <ItemTemplate>

        <DataTemplate>
          <StackPanel>

            <TextBlock Text="{Binding GroupName}"/>
            <ListView  ItemsSource = "{Binding InnerList}">

              <ItemTemplate>

                <DataTemplate>

                   <TextBlock Text="{Binding}"/>

                </DataTemplate>

              </ItemTemplate>

            </ListView>

          <StackPanel>

        </DataTemplate>

      </ItemTemplate>

    </ListView>


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, July 31, 2014 11:43 AM
    Moderator
  • Hi.

    <GridView ItemsSource={Binding RootItems}>
     <GridView.ItemTemplate>
      <DataTemplate>
         <GridView ItemsSource={Binding FirstCollection}>
            <GridView.ItemTemplate>
               <DataTemplate>
                   <TextBlock Text={Binding Title}/>
               </DataTemplate>
            <GridView.ItemTemplate>
         </GridView>
         <ListView ItemsSource={Binding SecondCollection}>
            <ListView.ItemTemplate>
               <DataTemplate>
                  <TextBlock Text={Binding name}/>
               </DataTemplate>
            <ListView.ItemTemplate>
         </GridView>
      </DataTemplate>
     <GridView.ItemTemplate>
    </GridView>
    
    
    

    //models:
    public class RootItem
    {
        public List<A> FirstCollection {get;set;}
        public List<B> SecondCollection {get;set;}
    }
    
    public class A
    {
        public string Title {get;set;}
    }
    public class B
    {
        public string Name {get;set;}
    }




    Thursday, July 31, 2014 11:44 AM