Xamarin.Forms Guides | Samples

Xamarin.Forms.ListView Class

An ItemsView that displays a collection of data as a vertical list.

See Also: ListView

Syntax

[Xamarin.Forms.RenderWith(typeof(Xamarin.Forms.Platform._ListViewRenderer))]
public class ListView : ItemsView<Cell>, IElementConfiguration<ListView>, IListViewController

Remarks

The following example shows a basic use:

C# Example

using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace FormsGallery
{
class ListViewDemoPage : ContentPage
    {
        class Person
        {
            public Person(string name, DateTime birthday, Color favoriteColor)
            {
                this.Name = name;
                this.Birthday = birthday;
                this.FavoriteColor = favoriteColor;
            }

            public string Name { private set; get; }

            public DateTime Birthday { private set; get; }

            public Color FavoriteColor { private set; get; }
        };

        public ListViewDemoPage()
        {
            Label header = new Label
            {
                Text = "ListView",
                FontSize = Device.GetNamedSize (NamedSize.Large, typeof(Label)),
                HorizontalOptions = LayoutOptions.Center
            };

            // Define some data.
            List<Person> people = new List<Person>
            {
                new Person("Abigail", new DateTime(1975, 1, 15), Color.Aqua),
                new Person("Bob", new DateTime(1976, 2, 20), Color.Black),
                // ...etc.,...
                new Person("Yvonne", new DateTime(1987, 1, 10), Color.Purple),
                new Person("Zachary", new DateTime(1988, 2, 5), Color.Red)
            };

            // Create the ListView.
            ListView listView = new ListView
            {
                // Source of data items.
                ItemsSource = people,

                // Define template for displaying each item.
                // (Argument of DataTemplate constructor is called for 
                //      each item; it must return a Cell derivative.)
                ItemTemplate = new DataTemplate(() =>
                    {
                        // Create views with bindings for displaying each property.
                        Label nameLabel = new Label();
                        nameLabel.SetBinding(Label.TextProperty, "Name");

                        Label birthdayLabel = new Label();
                        birthdayLabel.SetBinding(Label.TextProperty,
                            new Binding("Birthday", BindingMode.OneWay, 
                                null, null, "Born {0:d}"));

                        BoxView boxView = new BoxView();
                        boxView.SetBinding(BoxView.ColorProperty, "FavoriteColor");

                        // Return an assembled ViewCell.
                        return new ViewCell
                        {
                            View = new StackLayout
                            {
                                Padding = new Thickness(0, 5),
                                Orientation = StackOrientation.Horizontal,
                                Children = 
                                {
                                    boxView,
                                    new StackLayout
                                    {
                                        VerticalOptions = LayoutOptions.Center,
                                        Spacing = 0,
                                        Children = 
                                        {
                                            nameLabel,
                                            birthdayLabel
                                        }
                                        }
                                }
                                }
                        };
                    })
            };

            // Accomodate iPhone status bar.
            this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5);

            // Build the page.
            this.Content = new StackLayout
            {
                Children = 
                {
                    header,
                    listView
                }
                };
        }
    }
}

XAML for Xamarin.Forms supports the following properties for the ListView class:

PropertyValue
HasUnevenRows

true or false, to indicate whether the items in the list all have the same height.

IsGroupingEnabled

true or false, to indicate whether grouping is enabled.

RowHeight

An integer that describes the height of the items in the list. This is ignored if HasUnevenRows is true.

Requirements

Namespace: Xamarin.Forms
Assembly: Xamarin.Forms.Core (in Xamarin.Forms.Core.dll)
Assembly Versions: 1.0.0.0, 1.1.0.0, 1.2.0.0, 1.3.0.0, 1.3.5.0, 1.4.0.0, 1.5.0.0, 2.0.0.0

The members of Xamarin.Forms.ListView are listed below.

See Also: Inherited members from Xamarin.Forms.ItemsView<Xamarin.Forms.Cell>

Public Constructors

Creates and initializes a new instance of the ListView class.
Creates and initializes a new instance of the ListView class, with the specified caching strategy.

Public Fields

static readonly
FooterPropertyBindableProperty. Backing store for the ListView.Footer property.
static readonly
FooterTemplatePropertyBindableProperty. Backing store for the ListView.FooterTemplate property.
static readonly
GroupDisplayBindingPropertyBindableProperty. Identifies the ListView.GroupDisplayBinding property.
static readonly
GroupHeaderTemplatePropertyBindableProperty. Identifies the ListView.GroupHeaderTemplate property.
static readonly
GroupShortNameBindingPropertyBindableProperty. Identifies the ListView.GroupShortNameBinding property.
static readonly
HasUnevenRowsPropertyBindableProperty. Identifies the ListView.HasUnevenRowsProperty property.
static readonly
HeaderPropertyBindableProperty. Backing store for the ListView.Header property.
static readonly
HeaderTemplatePropertyBindableProperty. Backing store for the ListView.HeaderTemplate property.
static readonly
IsGroupingEnabledPropertyBindableProperty. Identifies the ListView.IsGroupingEnabledProperty property.
static readonly
IsPullToRefreshEnabledPropertyBindableProperty. Backing store for the ListView.IsPullToRefreshEnabled property.
static readonly
IsRefreshingPropertyBindableProperty. Backing store for the ListView.IsRefreshing property.
static readonly
RefreshCommandPropertyBindableProperty. Backing store for the ListView.RefreshCommand property.
static readonly
RowHeightPropertyBindableProperty. The backing store for the ListView.RowHeightProperty property.
static readonly
SelectedItemPropertyBindableProperty. Identifies the ListView.SelectedItemProperty property.
static readonly
SeparatorColorPropertyBindableProperty. Backing store for the ListView.SeparatorColor property.
static readonly
SeparatorVisibilityPropertyBindableProperty. Backing store for the ListView.SeparatorVisibility property.

Public Properties

FooterObject. Gets or sets the string, binding, or view that will be displayed at the bottom of the list view.
FooterTemplateDataTemplate. Gets or sets a data template to use to format a data object for display at the bottom of the list view.
GroupDisplayBindingBindingBase. Gets or sets the binding to use for display the group header.
GroupHeaderTemplateDataTemplate. Gets or sets a DataTemplate for group headers.
GroupShortNameBindingBindingBase. Gets or sets a binding for the name to display in grouped jump lists.
HasUnevenRowsBoolean. Gets or sets a Boolean value that indicates whether this ListView element has uneven rows.
HeaderObject. Gets or sets the string, binding, or view that will be displayed at the top of the list view.
HeaderTemplateDataTemplate. Gets or sets a data template to use to format a data object for display at the top of the list view.
IsGroupingEnabledBoolean. Gets or sets whether or not grouping is enabled for ListView.
IsPullToRefreshEnabledBoolean. Gets or sets a value that tells whether the user can swipe down to cause the application to refresh.
IsRefreshingBoolean. Gets or sets a value that tells whether the list view is currently refreshing.
RefreshCommandICommand. Gets or sets the command that is run when the list view enters the refreshing state.
RowHeightInt32. Gets or sets a value that represents the height of a row.
SelectedItemObject. Gets or sets the currently selected item from the ListView.ItemsSource.
SeparatorColorColor. Gets or sets the color of the bar that separates list items.
SeparatorVisibilitySeparatorVisibility. Gets or sets a value that tells whether separators are visible between items.

Public Methods

BeginRefresh()
Enters the refreshing state by setting the ListView.IsRefreshing property to true.
EndRefresh()
Exits the refreshing state by setting the ListView.IsRefreshing property to false.
On<T>() : IPlatformElementConfiguration<T, ListView>
ScrollTo(Object, ScrollToPosition, Boolean)
Scrolls the ListView to the item.
ScrollTo(Object, Object, ScrollToPosition, Boolean)
Scrolls the ListView to the item in the group

Protected Methods

override
CreateDefault(Object) : Cell
Creates an instance of the default visual representation of an item.
override
OnBindingContextChanged()
override
OnSizeRequest(Double, Double) : SizeRequest
Method that is called when a size request is made..
override
SetupContent(Cell, Int32)
Application developers override this method to register event handlers for list view items.
override
UnhookContent(Cell)
Application developers override this method to unregister event handlers for list view items that they registered in ListView.SetupContent(Cell,int).

Public Events

ItemAppearingOccurs when the visual representation of an item is being added to the visual layout.
ItemDisappearingOccurs when the visual representation of an item is being removed from the visual layout.
ItemSelectedEvent that is raised when a new item is selected.
ItemTappedEvent that is raised when an item is tapped.
RefreshingEvent that is raised when the list view refreshes.

Explicitly Implemented Interface Members