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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With