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

By | July 6, 2017
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:

For this I added this namespace in my XAML:

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

Cool! Yet another Xamarin Forms gem I didn’t know about! 🙂

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

  1. ZiChao Zhang

    Sir, you can try UnFocused event, it worked same.
    Forgive my poor english, i hope you can see it.

    1. PieEatingNinjas Post author

      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.

  2. Pingback: The week in .NET – Command Line Parser Library, .NET South East | .NET Blog

Leave a Reply