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?
Posts
Thank You!!