question

DonRea-6311 avatar image
0 Votes"
DonRea-6311 asked LeonLu-MSFT edited

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

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?


dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DonRea-6311 avatar image
0 Votes"
DonRea-6311 answered LeonLu-MSFT edited

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>





· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your sharing,

0 Votes 0 ·