Xamarin Forms Bindable Picker

Introduction

This blog post has been replaced and updated.  Please see the updated post:  https://oceanware.wordpress.com/2016/08/12/xamarin-forms-bindable-picker-v2/

New post has new code, new features, and project on gitgub.

Close

Have a great day.

Just a grain of sand on the worlds beaches.

39 comments

  1. Tomasz Kowalczyk

    Hi,

    great work, but I have some problem with displaying the Abbreviation property after Item is Selected, this value is not showing because the Name property is displayed, are you sure that everything is alright here?

    thanks!

    Like

  2. Tomasz Kowalczyk

    Hi Karl, I got it from above article. I also had to change:

    DisplayMemberPath=”Name”
    SelectedValuePath=”Abbreviation”
    ItemsSource=”{Binding Months}”
    SelectedItem=”{Binding MonthSelected, Mode=TwoWay}” before was SelectedValue but it was not binding then.

    Like

  3. Karl

    Tomasz,

    Do you have the latest BindablePicker??

    If you bind to selecteditem, you do not bind to selectedvalue. Works just like the WPF ComboBox.

    This example is from my repro here: https://github.com/Oceanware/XamlPowerToysDemo

    DisplayMemberPath=”Name”
    SelectedValuePath=”Abbreviation”
    ItemsSource=”{Binding Path=Countries}”
    SelectedValue=”{Binding Path=Person.Country, Mode=TwoWay}”

    Like

  4. prakash

    Hi
    Great work! You solution only works one time for me. So, If I update my itemssource with new items [the underlying IList is Observablecollection in my case] the picker is never updated! I debugged it and and found that “OnItemsSourceChanged” is never called when I add/remove items to the observablecollection. How do we change the picker values after the first binding thru the observablecollection?
    Thx!

    Liked by 1 person

    • Karl

      Can you post a simple example in a github repro so I can see what your scenario is.

      Be glad to work the issue and make any required modifications.

      Like

    • Karl

      prakash, I just published a video on this blog post that I hope will help explain how to get the most from this control.

      Like

    • Kevin Lloyd

      You’re having a problem similar to what I have. This picker doesn’t subscribe to INotifyCollectionChanged on ItemSource, only INotifyPropertyChanged. Therefore, you would have to do a full replacement of whatever ItemSource is bound to.

      e.g.:
      Countries = new List(countries); // it has to be a new instance since OnItemsSourceChanged is doing an equality check.

      However, my other problem is actually inside of OnItemsSourceChanged.
      In my view model, if I update ItemSource and SelectedItem, my SelectedItem is not being reflected in the picker.

      This seems to be because picker.Items.Clear() is triggering OnSelectedIndexChanged (with SelectedIndex = -1) and subsequently clearing out my SelectedItem.

      Back inside of OnItemsSourceChanged() it tries to initiate picker.InternalSelectedItemChanged() to update the SelectedIdex using SelectedItem, but it has already been nulled out.

      Any chance of getting an update for this bug?

      Like

      • Karl

        Hi Keven,

        Can you post a sample project on github so I can look at it.

        All of my projects work but I want to see your exact scenario and correct any issues asap.

        Thank you,

        Karl

        Like

      • Karl

        I just replied on your gist. Check out the usage of SelectedItem and SelectedValue in the above Example Usage.

        You’re using SelectedItem and DisplayMemberPath together, this is not supported.

        Karl

        Like

    • Kevin Lloyd

      Karl, even with your suggestion I get the same results. The issue is that “picker.Items.Clear();” triggers “OnSelectedIndexChanged”, with a SelectedIndex of -1 (since the list is now clear).

      This will then go through and null out both SelectedItem and SelectedValue. I believe that you need to disable nested calls before you clear Items.

      Please note that this doesn’t happen on initial load. It happens when I update the list after it’s been loaded (think in terms of a “reload” button what will fetch a new ItemSource and SelectedItem/SelectedValue from a server).

      If you set up a GitHub project for the sample you have in the video, I should be able to give you a PR that duplicates my issue with that.

      Like

      • Karl

        Kevin,

        I need to do this control correctly. On Thursday night, I’ll put it in a repro with full unit tests. I’ll include your scenarios and an example project.

        My bad on not doing this day one.

        Thank you for your feedback and bug report, I greatly appreciate it.

        Best,

        Karl

        Like

      • Karl

        Kevin,

        Two scenarios:

        INotifyCollectionChanged – I’m working on this now.

        Refreshing or Replacing the collection and then setting the selected item in code already works today.

        When the items source is replaced, I have to clear out selected index, selected item, and selected value. Why? Because I have no way of knowing if the new data will even have that object, additionally, the currently selected item instance would no longer be in the new items source collection.

        Did I miss something?

        Best,

        Karl

        Like

      • Karl

        Kevin,

        I’m putting together a project that will demonstrate all the scenarios I know of. I’ll include the ones you have mentioned as well.

        Thanks,

        Karl

        Like

  5. Aimoré Sá Costa

    Hello Karl,
    amazing post!

    I have one question, how can I create a “language picker”, that uses my RESX ressource files to change to the language clicked.

    Like

    • Karl

      I don’t know how Xamarin Forms selects the language at runtime.

      You’ll need to post a question on the Xamarin Forms Forum for this one.

      Best,

      Karl

      Like

      • Aimoré Sá Costa

        I mean, my app already can identify the “system location” and set the correct language.

        I was just wondering, how to change the language in my language picker.

        Thanks for the quick reply 🙂
        Aimoré

        Like

      • Karl

        Aimoré,

        Sorry, I don’t understand the question. Do you have a simple github repro that I can look at?

        Best,

        Karl

        Like

  6. kaushal devluk

    hi i used this bindable picker anywhere in my whole project but i can not get selected value which i assign in server side any have a suggetion then help me

    Like

    • Karl

      Thank you for calling this out.

      One problem, the new Picker is still missing properties and features that my BindablePicker has.

      I’ll update mine when they release.

      Best,

      Karl

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s