Bind to Xamarin Picker but only update value after hitting Done on iOS


Sometimes you stumble upon these gems that you didn’t know they existed. Like I did today…

The ‘problem’ with the Picker

You might have noticed that when you are binding to the SelectedItem of a Picker in Xamarin Forms, that on iOS the value of the bound property already changes when the user is scrolling through the list of options, even without explicitly having tapped the ‘Done’ button.

Now in a particular app I’m developing, I -the product owner actually- didn’t want this behavior. Instead, the bound property should change its value ONLY when the user taps the Done button in the Picker. And so I started to try some stuff out…

Platform specifics to the rescue

Eventually, after trying some stuff and after I lost already too much time on this to be honest, I came across to the ‘Picker.UpdateMode’ platform-specific. And with this, I can define when the change on my property should be triggered: Immediately  (the default) or WhenFinished  (when the user tapped the ‘Done’ button). In XAML this looks something like this:

Picker.UpdateMode in XAML

<Picker ItemsSource="{Binding Options}"
    SelectedItem="{Binding Result}"
    ItemDisplayBinding="{Binding Label}"
    iOSSpecific:Picker.UpdateMode="WhenFinished" />

For this I added this namespace in my XAML:

iOSSpecific namespace in XAML

xmlns:iOSSpecific="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"

If we would want to do the same thing in code, we can do it as follows:

SetUpdateMode in code

myPicker.On<Xamarin.Forms.PlatformConfiguration.iOS>().SetUpdateMode(UpdateMode.WhenFinished);

Cool! Yet another Xamarin Forms gem I didn’t know about! :-)

Join the discussion

Archived comments (10)

Archived comments from the original blog, preserved here for reference. Join the discussion above.

  1. ZiChao Zhang
    Sir, you can try UnFocused event, it worked same. Forgive my poor english, i hope you can see it.
  2. PieEatingNinjasauthorIn reply
    Indeed, you can. That was one of the work arounds I wrote before I found this solution. It felt a bit like a hack an didn't play nice with databinding neither (I ended up with using 2 properties on my viewmodel and stuff). With this solution, in combination with databinding, I don't have the feeling I'm hacking my way around as this is a thing from the framework itself.
  3. vlad
    Any way to include a cancel button also?
  4. Pablo Siller
    Excellent solution, thanks for sharing.
  5. Chris Knight
    This!!!!!! Thanks for sharing!
  6. Hzap
    You're great!!!!! I have been wondering for this since long many weeks
  7. Richard Canoa
    Excelente Solución, gracias.
  8. primehalo
    So this only works on Picker, not DatePicker or TimePicker?
  9. Jamin FrederickIn reply
    No, looks like this is still considered an enhancement for DatePicker or TimePicker: https://bugzilla.xamarin.com/show_bug.cgi?id=57859