locked
How to create a sub listview inside a listview with data binding? RRS feed

  • Question

  • User156697 posted

    Hello,

    I want to bind an observable collection of a List of items something like ObservableCollection> with that xaml code:

    The binding on the sub listview is not working and I don't know what to use to solve it. Is there a better solution than Listview to use?

    Friday, September 18, 2015 7:52 AM

Answers

  • User1175 posted

    Hi @Blondin

    Give it a try to following code snippet: Your data hierarchy should be like this:

     public class YourViewModel
        {
            //This is for Parent List
            private IList<ParentListClass> _parentItems;
            public IList<ParentListClass> ParentItems
            {
                get { return _parentItems; }
                set { _parentItems = value; }
            }
    
        }
    
        public class ParentListClass
        {
            public string ParentTitle { get; set; }
    
            //This is for child List
            public IList<ChildListClass> ChildItems { get; set; }
        }
    
        public class ChildListClass
        {
            public string ChildTitle { get; set; }
            public string ChildSubTitle { get; set; }
        } 
    

    Your XAML code binding with list as following:

            <ListView x:Name="ReferencesNew" ItemsSource="{Binding ParentItems}">
                    <ListView.ItemTemplate>
                      <DataTemplate>
                          <ListView ItemsSource="{Binding ChildItems}">
                             <ListView.ItemTemplate>
                                 <DataTemplate>
                                      <TextCell Text="{Binding ChildTitle}" Detail="{Binding ChildSubTitle }"/>
                                 </DataTemplate>
                             </ListView.ItemTemplate>
                          </ListView>
                      </DataTemplate>
                  </ListView.ItemTemplate>
              </ListView>
    

    Cheers!! RIYAZ

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, September 22, 2015 7:56 AM

All replies

  • User156697 posted

    Sorry for the previous displayed code:

       ``<ListView x:Name="ReferencesNew" ItemsSource="{Binding RefNew}">
        ``                <ListView.ItemTemplate>
        ``                  <DataTemplate>
        ``                      <ListView ItemsSource={Binding **?**}>
        ``                         <ListView.ItemTemplate>
        ``                             <DataTemplate>
        ``                                  <TextCell Text="{Binding Ref}" Detail="{Binding Codification}"/>
        ``                             </DataTemplate>
        ``                         </ListView.ItemTemplate>
        ``                      </ListView>
        ``                  </DataTemplate>
        ``              </ListView.ItemTemplate>
        ``          </ListView>
    
    Friday, September 18, 2015 8:00 AM
  • User1175 posted

    Hi @Blondin

    Give it a try to following code snippet: Your data hierarchy should be like this:

     public class YourViewModel
        {
            //This is for Parent List
            private IList<ParentListClass> _parentItems;
            public IList<ParentListClass> ParentItems
            {
                get { return _parentItems; }
                set { _parentItems = value; }
            }
    
        }
    
        public class ParentListClass
        {
            public string ParentTitle { get; set; }
    
            //This is for child List
            public IList<ChildListClass> ChildItems { get; set; }
        }
    
        public class ChildListClass
        {
            public string ChildTitle { get; set; }
            public string ChildSubTitle { get; set; }
        } 
    

    Your XAML code binding with list as following:

            <ListView x:Name="ReferencesNew" ItemsSource="{Binding ParentItems}">
                    <ListView.ItemTemplate>
                      <DataTemplate>
                          <ListView ItemsSource="{Binding ChildItems}">
                             <ListView.ItemTemplate>
                                 <DataTemplate>
                                      <TextCell Text="{Binding ChildTitle}" Detail="{Binding ChildSubTitle }"/>
                                 </DataTemplate>
                             </ListView.ItemTemplate>
                          </ListView>
                      </DataTemplate>
                  </ListView.ItemTemplate>
              </ListView>
    

    Cheers!! RIYAZ

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, September 22, 2015 7:56 AM
  • User156697 posted

    Hello rzee7, thanks for your answer. . I try grouped listview and it's working too with the intial object I described.

    Wednesday, September 23, 2015 7:32 AM
  • User184864 posted

    Hi. I am also wondering about this one. I have nested a listview inside a listview more or less like @Blondin has done. It works great on UWP (with multiple "Main"-rows and sub-rows on some or all "mains".

    On Android if I add one "main"-row with a sub-row. However trying to add another row to the "main" gets me the Exception:

    "System.NotSupportedException: Unable to activate instance of type Xamarin.Forms.Platform.Android.ListViewAdapter from native handle 0x7d000045 (key_handle 0x41c3ec28)."

    Thursday, October 20, 2016 1:20 PM
  • User261343 posted

    Hi @rzee7 , I get same error as @JoakimMnsson.

    System.NotSupportedException: Unable to activate instance of type Xamarin.Forms.Platform.Android.ListViewAdapter from native handle 0x2c90005d (key_handle 0x428e1330).

    When I enter first item, it only displays child element and for second time, it shows above error.

    Please help.

    Monday, November 7, 2016 7:34 AM
  • User1175 posted

    Hi @trishelwood

    I'm not sure what causes an issue, I'd suggest two things here as following:

    1. Try with static data as I have given an answer above.
    2. Explain about your data flow insertion, then I will be able to solve it.

    Cheers!! {rzee}

    Tuesday, November 8, 2016 6:43 AM
  • User203004 posted

    @rzee7 @Blondin

    Hi all, I have the same scenario.I need a sub list view in each cell of a list view . I have used the same code as mentioned above.But my issue is ,main list view is listing but in each cell of main list view only first element of sublistview is listing.

    < ListView RowHeight="160" ItemsSource="{Binding GroupList,Mode=TwoWay}" SeparatorVisibility="None" BackgroundColor="#F2F6F8" HorizontalOptions="Start">

              <StackLayout Padding="10">
                <StackLayout Orientation="Horizontal" BackgroundColor="#ffffff" Padding="10" HorizontalOptions="FillAndExpand">
    
    
                  <BoxView WidthRequest="2" BackgroundColor="#D7D7D7"></BoxView>
                  <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                    <ListView RowHeight="160" BackgroundColor="#ffffff" ItemsSource="{Binding BatchList,Mode=TwoWay}" SeparatorVisibility="None"  HorizontalOptions="FillAndExpand">
                      <ListView.ItemTemplate>
                        <DataTemplate>
                          <ViewCell>
                            <StackLayout Orientation="Vertical"  HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                              <Button Text="{Binding Value}" BackgroundColor="#ffff00"/>
                            </StackLayout>
    
                          </ViewCell>
                        </DataTemplate>
                      </ListView.ItemTemplate>
                    </ListView >
    
    Tuesday, November 29, 2016 2:34 PM
  • User1175 posted

    Hi @SujaB

    Why not use Grouping instead nested ListView ?

    Thursday, December 1, 2016 8:37 AM
  • User203004 posted

    @rzee7 Hi

    Could you please share some idea about grouping inside Listview? Any sample code or link

    Friday, December 2, 2016 6:53 AM
  • User1175 posted

    @SujaB You can try This see if this what you are looking for.

    Cheers {rzee}

    Friday, December 2, 2016 9:54 AM
  • User296434 posted

    I wonder how to write it in the code.

    Wednesday, February 15, 2017 7:47 PM
  • User160621 posted

    I think @rzee7 solution not working for {Binding Path=.} child listview .It gives ParentListClass so this must be a bug.

    Saturday, July 1, 2017 8:18 AM
  • User281430 posted

    M. RIYAZ canyou share the code for nested listview im new user for xamarin forms pls help me .bcoz i want to this tipe of view.

    Plz Reply Fast

    Friday, July 21, 2017 6:12 AM
  • User337814 posted

    I'm also getting an error, is C# coding the only way? I was hoping to write my entire UI in XAML. Grouping seeing a bit limited, my Model has several lists: { public class Survey { public int SurveyID { get; set; } public string LastName{ get; set; } public string FirstName{ get; set; }
    public Form Form { get; set; }
    }

    public class Form { public int FormID { get; set; } public List Categories { get; set; } }

    public class Category { public int CategoryID { get; set; } public string Description { get; set; } public List Questions { get; set; } }

    public class Question { public int QuestionID { get; set; } public string Description { get; set; } public bool IsModified { get; set; } public int SelectedAnswerID { get; set; } public List Answers { get; set; } }

    public class Answer { public int QuestionID { get; set; } public int AnswerID { get; set; } public string Description { get; set; } } }

    Using @rzee7 Code, I was about to get values back for the first ListView Categories. As soon I added the Second listview for questions, I get System.NotSupportedException: Unable to activate instance of type.

    Monday, July 24, 2017 10:47 AM
  • User337814 posted

    I'm also getting an error, is C# coding the only way? I was hoping to write my entire UI in XAML. Grouping seeing a bit limited, my Model has several lists: { public class Survey { public int SurveyID { get; set; } public string LastName{ get; set; } public string FirstName{ get; set; }
    public Form Form { get; set; }
    }

    public class Form { public int FormID { get; set; } public List Categories { get; set; } }

    public class Category { public int CategoryID { get; set; } public string Description { get; set; } public List Questions { get; set; } }

    public class Question { public int QuestionID { get; set; } public string Description { get; set; } public bool IsModified { get; set; } public int SelectedAnswerID { get; set; } public List Answers { get; set; } }

    public class Answer { public int QuestionID { get; set; } public int AnswerID { get; set; } public string Description { get; set; } } }

    Using @rzee7 Code, I was able to get values back for the first ListView Categories. As soon I added the Second listview for questions, I get System.NotSupportedException: Unable to activate instance of type.

    Monday, July 24, 2017 10:48 AM
  • User365438 posted

    Hello i have a question !! I have a listview inside a listview like rzee7 example but i want to scroll inside inner listview.I have made the inner listview to 400 heightrequest.Is it possible ???

    Monday, February 12, 2018 10:10 AM
  • User351573 posted

    @"sumitsisodia.9614" said: M. RIYAZ canyou share the code for nested listview im new user for xamarin forms pls help me .bcoz i want to this tipe of view.

    Plz Reply Fast

    Do you complete this requirement? Do you have any sample codes for nested listview?

    Wednesday, March 21, 2018 12:41 PM
  • User365053 posted

    @sumitsisodia.9614 have you done this NestedListview?If yes can I have your inputs plz..

    Friday, April 20, 2018 9:33 AM
  • User345835 posted

    Hi Try this Xamarin Cross Plateforme project : "ListViewWithSubListView" in my github named "github.com/ishrakland"

    to avoid the problem of nested Lists

    Tuesday, July 17, 2018 2:15 PM
  • User345062 posted

    Hello,

    In which memory(Internal or External memory) Xamarin Listview data is saved?

    Friday, December 28, 2018 6:30 AM
  • User74 posted

    @PankajNandurkar said: Hello,

    In which memory(Internal or External memory) Xamarin Listview data is saved?

    Haven't you already asked this on several other threads? What part of the answers you received were unclear?

    Friday, December 28, 2018 10:31 AM
  • User345062 posted

    @JohnHair said:

    @PankajNandurkar said: Hello,

    In which memory(Internal or External memory) Xamarin Listview data is saved?

    Haven't you already asked this on several other threads? What part of the answers you received were unclear?

    I am not addressing only Listview but other Xamarin controls as well.

    It is clear that OS insulate app data, but need to know whether Xamarin stores any app data for conversion purposes (App conversion from Cross Platform into their respective Android Solution or iOS Solution)

    Just need an assurance that no app data is stored or access by any other application.

    Friday, December 28, 2018 10:46 AM
  • User89714 posted

    @PankajNandurkar - As @JohnHair mentioned, you have raised this question elsewhere. Please do not hijack existing threads, nor ask the same question repeatedly.

    Friday, December 28, 2018 11:54 AM
  • User382027 posted

    Im having 2 section, for 1 section im binding static data to ChildTitle ,

    for another sction i need to bind dynamic data(i.e That was coming from api with n number of values)

    how can i do that????

    Friday, April 5, 2019 11:34 AM
  • User370959 posted

    Those of you who stumbled on this site, looking for solution. ListView inside the ListView is not supported(I guess in older version of xamarin forms it was).

    https://stackoverflow.com/questions/35223540/listview-inside-listview-xamarin-forms

    Monday, March 2, 2020 3:35 PM