Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms Picker SelectedItem Binding

The Xamarin Forms doc Xamarin.Forms.Picker.SelectedItem says there is a public property SelectedItem for Picker. However, I get an error when I try to bind to it. The picker is not very useful if you have to manually handle the SelectedIndex property.

Tony

like image 656
Tony Lugg Avatar asked Jan 19 '17 18:01

Tony Lugg


1 Answers

No need to manually handle the SelectedIndex. You can use the Picker's SelectedItem property. Just make sure your types are the same. For example, if your ItemsSource is bound to a property:

BookTitles List<string> { get; set; }

your SelectedItem has to be something like:

SelectedBookTitle string { get; set; }

Make sure to set the SelectedBookTitle value to show a title when the page is first shown. Do not forget to set Mode to TwoWay on the SelectedItem Binding. for example:

<Picker ItemsSource="{Binding BookTitles}" SelectedItem="{Binding 
SelectedBookTitle, Mode=TwoWay}" />

This will ensure the title is shown when the page is first displayed, and keeps the value of SelectedBookTitle equal on the Page and codebehind/viewmodel.

No need to use behaviours in this example.

like image 143
Daniel Bikker Avatar answered Nov 07 '22 16:11

Daniel Bikker