Introduction

This article demonstrates how to create a Dynamic ListView Application using C# and XAML in Xamarin.Forms.
Let's start.

Step 1

Open Visual Studio and go to New Project >> installed >> Visual C# >> Cross-Platform.

Select Cross-Platform App, then give your project a name and location.


Step 2

After project creation, update the following NuGet Packages to your project.

Xamarin.Forms

Go to Solution Explorer and select your solution. Right-click and select "ManageNuGett Packages for Solution".



Now, select the following NuGet package and select your project to update it.

Step 3

Next, add an image to open Solution Explorer >> projectName.Android >> Resources >> Right click the drawable >>Add >> Existing Item and add the image.





Step 4

Open Solution Explorer >> Project Name >> Mainpage.xaml. Open the Design View of this page.



The code is given below.



XAML Code
  1. <StackLayout Padding="0,20,0,0">
  2. <Label Text="Dynamic Resizing Demo" FontAttributes="Bold" HorizontalOptions="Center" />
  3. <ListView x:Name="listView" HasUnevenRows="true">
  4. <ListView.ItemTemplate>
  5. <DataTemplate>
  6. <ViewCell>
  7. <StackLayout Padding="15,5,5,5" Orientation="Horizontal">
  8. <Image Source="Image.jpg" HeightRequest="50">
  9. <Image.GestureRecognizers>
  10. <TapGestureRecognizer Tapped="OnImageTapped" />
  11. </Image.GestureRecognizers>
  12. </Image>
  13. <Label Text="Flower" VerticalOptions="Center" />
  14. </StackLayout>
  15. </ViewCell>
  16. </DataTemplate>
  17. </ListView.ItemTemplate>
  18. </ListView>
  19. </StackLayout>
Step 4

Open Solution Explorer >> Project Name >> Mainpage.xaml.cs. Open the Design View of this page.



The code is given below.



C# Code
  1. public partial class MainPage : ContentPage
  2. {
  3. public MainPage()
  4. {
  5. BindingContext = this;
  6. InitializeComponent();
  7. var items = Enumerable.Range(0, 10);
  8. listView.ItemsSource = items;
  9. }
  10. void OnImageTapped(object sender, EventArgs args)
  11. {
  12. var image = sender as Image;
  13. var viewCell = image.Parent.Parent as ViewCell;
  14. if (image.HeightRequest < 250)
  15. {
  16. image.HeightRequest = image.Height + 100;
  17. viewCell.ForceUpdateSize();
  18. }
  19. }
  20. }
Step 5

Next, open Solution Explorer >> ProjectName(Portable) >> Right Click >> Add >> Class.



The Dialogue Box will open. Add the Class and give a name.



Step 6

Open Solution Explorer >> Project Name >> Class.cs. Open the Design View of this page.



The code is given below.



C# Code
  1. public class MyViewCell : ViewCell
  2. {
  3. public MyViewCell()
  4. {
  5. var image = new Image
  6. {
  7. Source = ImageSource.FromFile("Image.jpg"),
  8. HeightRequest = 50,
  9. };
  10. var tapGestureRecognizer = new TapGestureRecognizer();
  11. tapGestureRecognizer.Tapped += (object sender, EventArgs e) => {
  12. if (image.HeightRequest < 250)
  13. {
  14. image.HeightRequest = image.Height + 100;
  15. ForceUpdateSize();
  16. }
  17. };
  18. image.GestureRecognizers.Add(tapGestureRecognizer);
  19. var stackLayout = new StackLayout
  20. {
  21. Padding = new Thickness(20, 5, 5, 5),
  22. Orientation = StackOrientation.Horizontal,
  23. Children = {
  24. image,
  25. new Label { Text = "Flower", VerticalOptions = LayoutOptions.Center }
  26. }
  27. };
  28. View = stackLayout;
  29. }
  30. }
  31. public class HomePageCS : ContentPage
  32. {
  33. public HomePageCS()
  34. {
  35. var listView = new ListView { HasUnevenRows = true };
  36. listView.ItemTemplate = new DataTemplate(typeof(MyViewCell));
  37. var items = Enumerable.Range(0, 10);
  38. listView.ItemsSource = items;
  39. Content = new StackLayout
  40. {
  41. Padding = new Thickness(0, 20, 0, 0),
  42. Children = {
  43. new Label {
  44. Text = "HasUnevenRows Dynamic Resizing Demo",
  45. FontAttributes = FontAttributes.Bold,
  46. HorizontalOptions = LayoutOptions.Center
  47. },
  48. listView
  49. }
  50. };
  51. }
  52. }
Step 8

Press F5 or Build and Run the application.

Output



Finally, we have successfully created a Xamarin.Forms DynamicListView.