How to bind Picker in Xamarin.Forms

11,893

Solution 1

This functionality is unfortunately missing from the standard component, but relatively easy to add as detailed at https://forums.xamarin.com/discussion/30801/xamarin-forms-bindable-picker. Using this derived component you will then be able to bind ItemsSource and SelectedItem properties. It's also relatively easy to add WPF-like DisplayMemberPath and ValueMemberPath properties if required.

Solution 2

Looks like this functionality is built into the regular Xamarin.Forms Picker now. It is currently in the pre-release NuGet package for version 2.3.4-pre1, but should be in the stable 2.3.4+ versions once it is released. Instead of binding to Items you bind to ItemsSource and SelectedItem.

Share:
11,893
why_vincent
Author by

why_vincent

I am a java developer.

Updated on June 04, 2022

Comments

  • why_vincent
    why_vincent about 2 years

    So I've been wanting to bind the items of two pickers in Xamarin.Forms to my ViewModel. I have mainly used binding for textfields, where I just write something like:

    <Label Text="{Binding CurrentDate}" />
    

    and simply by setting the binding context, defining a property in the viewmodel

        public System.DateTime CurrentDate{
            get { return currentDate; }
            set { currentDate = value; PropertyChanged(this, new PropertyChangedEventArgs("CurrentDate")); }
        }
    

    I am done binding. Now I have two pickers. The pickers represent a map/dictionary. Dictionary>. "A" is mapped to {"1","2"} and "B" is mapped to {"4","5"}. The first picker should show "A" and "B" as options. The second one should display the values associated with the chosen value from the first picker.

    So there are two questions. 1) How do I bind a picker? 2) How do I bind a picker that has data that depends on another pickers selection?

    I tried

    <Picker Items="{Binding ItemsA}"></Picker>
    

    With a matching property

        public List<string> ItemsA
        {
            get { return itemsA;}
            set { itemsA = value;PropertyChanged(this, new PropertyChangedEventArgs("ItemsA")); }
        }
    

    Am I missing out on something here? Help would be appreciated.

  • why_vincent
    why_vincent about 8 years
    Really? I am a bit new to this but Mvvm for me is pretty much all or nothing. I see no point in having an almost-fully separated view. I'll check out this component. Very odd that such a basic graphical element does not support this. Is this true for most Xamarin.Forms components?
  • Ben Jackson
    Ben Jackson about 8 years
    I agree - it did seem like a shocking omission when I first encountered it, but it's not the tip of an iceberg. With the addition of a BindablePicker control and a Repeater control (for repeating templated content based on an ItemsSource) I have found Xamarin Forms a pretty capable MVVM platform, especially in conjunction with the free DevExpress grid, and I have now relatively easily ported some substantial Microsoft XAML based applications without too many compromises.
  • Nitesh
    Nitesh almost 7 years
    its released now!