How can I display nested item lists in Xamarin Forms?

I am trying to get Xamarin Forms to display a list in a list. I have seen other people say that a ListView nested in a ListView is not supported. I tried to add a control to the ListView and bind the nested list to the control, but my application always crashes when I try to bind it.

I also found that some people suggest using grouping. DOES grouping works, however, the problems that I found with this, I can not use in the grouping section. Also, the grouping will not be updated in real time (this is a requirement).

For the end result, I would like to do something simple:

  • ApartmentName1
  • Address
  • Manager Phone Number
  • Tenant 1, phone number
  • Tenant 2, phone number
  • Tenant 3, phone number
  • ApartmentName2
  • Address
  • Manager Phone Number
  • Tenant 1, phone number
  • Tenant 2, phone number
  • Tenant 3, phone number

The following is an example of an object that I would like to display in my Xamarin Forms project.

public class Apartment { public string ApartmentName { get; set; } public string Address { get; set; } public string ManagerPhoneNumber { get; set; } public List<Tenant> Tenants { get; set; } } public class Tenant { public string FullName { get; set; } public string PhoneNumber { get; set; } public DateTime ContractExpireDate { get; set; } } 

Here is the Haml I was trying to work with

 <ContentView> <StackLayout> <ListView x:Name="apartmentListView" HasUnevenRows="True"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <ViewCell.View> <StackLayout VerticalOptions="StartAndExpand"> <StackLayout Orientation="Horizontal" BackgroundColor="Gray"> <Label HorizontalOptions="StartAndExpand" Text="{Binding ApartmentName}" /> <Label HorizontalOptions="End" Text="{Binding Address}" /> </StackLayout> <Label Text="{Binding ManagerPhoneNumber}" /> <control:TenantViewTemplate Tenants="{Binding Tenants}" /> </StackLayout> </ViewCell.View> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackLayout> </ContentView> 

I am only trying to show a list containing a list. I am open to any suggestions on how to do this using Xamarin forms.

+6
source share
2 answers

I would say skip XAML and just lay it out the way you want in the code.

Use StackLayouts and / or Grids and don’t rely on half-list binding of ListViews. Several times I started with ListView just to get to the point I popped up and went with ForEachLoop. You can do anything you like in the code you can do in XAML, and in the end you are likely to be happier.

+1
source

If I were going to display a list of dissimilar elements, I would probably use a TableView instead of a ListView. TableViews are not tied to a database, so you need to create them manually, but they allow you to flexibly use different types of cells depending on the data displayed on each row.

If you want to stick to a grouped ListView, you can create a custom group header view containing a button (or other element) that can respond to click gestures.

+4
source

All Articles