ListView
A ListView is a view for presenting scrollable lists of data.
<ListView x:Name="FruitListView" ItemsSource="{Binding Fruits}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding FruitName}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
A ListView is populated using "ItemsSource" property which takes any collection implementing IEnumerable.
using System.Collections.ObjectModel;
namespace listview;
public partial class MainPage : ContentPage
{
public class Fruit
{
public string FruitName { get; set; }
}
ObservableCollection fruits = new ObservableCollection();
public ObservableCollection Fruits { get { return fruits; } }
public MainPage()
{
InitializeComponent();
fruits.Add(new Fruit() { FruitName = "Apple" });
fruits.Add(new Fruit() { FruitName = "Orange" });
fruits.Add(new Fruit() { FruitName = "Banana" });
fruits.Add(new Fruit() { FruitName = "Grape" });
fruits.Add(new Fruit() { FruitName = "Mango" });
FruitListView.ItemsSource = fruits;
}
}
DataSource as part of XAML
A ListView can be populated with an array of strings in XAML. The sam example using XAML is shown below:
<ListView>
<ListView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Apple</x:String>
<x:String>Orange</x:String>
<x:String>Banana</x:String>
<x:String>Grape</x:String>
<x:String>Mango</x:String>
</x:Array>
</ListView.ItemsSource>
</ListView>
Download