简体   繁体   中英

How can I display a nested list of items in Xamarin Forms?

I'm trying to get Xamarin Forms to display a list within a list. I've seen other people say that a ListView nested within a ListView is not supported. I've tried to add a control inside the ListView, and bind the nested list to the control, but that always crashes my application whenever I try to bind to it.

I've also found that some people suggest to use a grouping. Grouping DOES work, however the problems I've found with that is I cannot tap on the grouping section. Also the grouping will not update in real-time (this is a requirement).

For the end result, I'd like something as simple as:

  • 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

Below is an example object that I'd 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 Xaml I've tried to get to work

<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, which contains a list. I'm open to any suggestions on how to do this through Xamarin Forms.

If I were going to display a list of dissimilar items I would probably use a TableView instead of a ListView. TableViews aren't databound, so you have to construct them manually, but they allow you the flexibility of having different types of cells depending on the data displayed in each row.

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

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

Use StackLayouts and\\or Grids and don't rely on the semi magical ListViews Databinding. Several times now I've started with a ListView only to get to a point later that I scraped it went with a ForEachLoop. You can do anything in code you can do in XAML and you'll likely be happier in the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM