Xamarin.Forms Guides | Samples

Xamarin.Forms.Picker Class

A View control for picking an element in a list.

See Also: Picker

Syntax

[Xamarin.Forms.RenderWith(typeof(Xamarin.Forms.Platform._PickerRenderer))]
public class Picker : View, IElementConfiguration<Picker>

Remarks

The visual representation of a Picker is similar to a Entry, but a picker control appears in place of a keyboard.

The following example shows the creation of a Picker.

C# Example

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

namespace FormsGallery
{
    class PickerDemoPage : ContentPage
    {
        // Dictionary to get Color from color name.
        Dictionary<string, Color> nameToColor = new Dictionary<string, Color>
        {
            { "Aqua", Color.Aqua }, { "Black", Color.Black },
            { "Blue", Color.Blue }, { "Fucshia", Color.Fucshia },
            { "Gray", Color.Gray }, { "Green", Color.Green },
            { "Lime", Color.Lime }, { "Maroon", Color.Maroon },
            { "Navy", Color.Navy }, { "Olive", Color.Olive },
            { "Purple", Color.Purple }, { "Red", Color.Red },
            { "Silver", Color.Silver }, { "Teal", Color.Teal },
            { "White", Color.White }, { "Yellow", Color.Yellow }
        };

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

            Picker picker = new Picker
            {
                Title = "Color",
                VerticalOptions = LayoutOptions.CenterAndExpand
            };

            foreach (string colorName in nameToColor.Keys)
            {
                picker.Items.Add(colorName);
            }

            // Create BoxView for displaying picked Color
            BoxView boxView = new BoxView
            {
                WidthRequest = 150,
                HeightRequest = 150,
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.CenterAndExpand
            };

            picker.SelectedIndexChanged += (sender, args) =>
                {
                    if (picker.SelectedIndex == -1)
                    {
                        boxView.Color = Color.Default;
                    }
                    else
                    {
                        string colorName = picker.Items[picker.SelectedIndex];
                        boxView.Color = nameToColor[colorName];
                    }
                };

            // 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,
                    picker,
                    boxView
                }
            };

        }
    }
}

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

PropertyValue
Items

A list of items with which to populate the picker.

SelectedIndex

An integer from 0 to 1 less than the count of items that are listed in Items. This element must be specified in a tag that appears lexically after Items.

Title

Text that represents the title of the picker.

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.4.0.0, 1.5.0.0, 2.0.0.0

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

See Also: View

Public Constructors

Initializes a new instance of the Picker class.

Public Fields

static readonly
ItemsSourcePropertyBindableProperty. Backing store for the Picker.ItemsSource property.
static readonly
SelectedIndexPropertyBindableProperty. Identifies the SelectedIndex bindable property.
static readonly
SelectedItemPropertyBindableProperty. Backing store for the Picker.SelectedItem property.
static readonly
TextColorPropertyBindableProperty. Backing store for the Picker.TextColor property.
static readonly
TitlePropertyBindableProperty. Identifies the Title bindable property.

Public Properties

ItemDisplayBindingBindingBase. Gets or sets a binding that selects the property that will be displayed for each object in the list of items.
[read-only]
ItemsIList<string>. Gets the list of choices.
ItemsSourceIList. Gets or sets the source list of items to template and display.
SelectedIndexInt32. Gets or sets the index of the selected item of the picker. This is a bindable property.
SelectedItemObject. Gets or sets the selected item.
TextColorColor. Gets or sets the text color.
TitleString. Gets or sets the title for the Picker. This is a bindable property.

Public Methods

On<T>() : IPlatformElementConfiguration<T, Picker>
Returns the platform-specific instance of this Picker, on which a platform-specific method may be called.

Public Events

SelectedIndexChangedRaised when the value of the SelectIndex property has changed.