Xamarin.Forms: How can I design a ListView ItemTemplate to accommodate a variable number of images?

Don Rea 31 Reputation points
2021-08-11T14:37:17.03+00:00

I have a ListView bound to a List of objects. Each object can have some number of images associated with it. Most have none, some have one, some have a number more than one that can vary with no specific maximum. I need to be able to show thumbnails of the images in the ListView items for those objects that have them. Without knowing in advance what the largest number of images associated with any one object is, how can I template and bind that?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,305 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Don Rea 31 Reputation points
    2021-08-11T18:57:20.723+00:00

    It turns out just binding the BindingContext property of an inner ListView to the pictures List property of the listed object is all I need:

    <ListView ItemsSource="{Binding .}" HasUnevenRows="True">
        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <StackLayout>
                <Label Text="{Binding Prompt}" />
                <ListView BindingContext="{Binding Pictures}" ItemsSource="{Binding .}">
                  <ListView.ItemTemplate>
                    <DataTemplate>
                      <ViewCell>
                        <Image
                            HeightRequest="80"
                            Source="{Binding PictureName, Converter={StaticResource PictureNameToImageSourceConverter}}" />
                      </ViewCell>
                    </DataTemplate>
                  </ListView.ItemTemplate>
                </ListView>
              </StackLayout>
            </ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>