Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Simple Picker Binding

shuffshuff Member ✭✭

I'm trying to understanding Binding and I created a simple project to Bind a Picker to an object.

Here is my XAML:
<?xml version="1.0" encoding="utf-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="PickerBinding.MainPage"> <StackLayout x:Name="mySL"> <Picker Title="Select Color" ItemsSource="{Binding colors}" ItemDisplayBinding="{Binding Name}" x:Name="MyPicker" VerticalOptions="CenterAndExpand" /> </StackLayout> </ContentPage>

And here is the code behind:

`
using System.Collections.Generic;
using System.ComponentModel;

using Xamarin.Forms;

namespace PickerBinding
{

public class Color
{
    public int Id { get; set; }
    public string Name { get; set; }
}


[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
    public List<Color> colors;

    public MainPage()
    {
        InitializeComponent();
        colors = new List<Color>();
        colors.Add(new Color { Id = 1, Name = "Red" });
        colors.Add(new Color { Id = 2, Name = "Yellow" });
        colors.Add(new Color { Id = 3, Name = "Green" });
        mySL.BindingContext = this;

    }
}

}
`

Why does this not work?

Tagged:

Posts

Sign In or Register to comment.