Forum Xamarin.Forms

Picker binding SelectedIndex to null?

With a picker that is populated with options ( especially when from an async source), that has binding to a selected item, the picker seems to expect the variable to be an int, in which case the default selection would be 0, or the first item until during the insertion of the options the selected item is updated if one has an existing value.
Wanting to not have an item selected initially I've bound the selectedIndex to a Nullable<int> - however I see visual studio shows an error:

Error Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

on the viewmodel line -1.

Is there a good way to deal with this that doesn't involve creating a 'blank' option?

(Or somehow only adding the binding from c# after the options have been retrieved in the viewmodel which seems a bit clunky perhaps?)

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    You do not want to show the Picker like following screenshot, when you do not get the value form an async source

    You can set Title for picker. Before your select the item, it will show the title

  • Andre_S_CAndre_S_C Member ✭✭

    Thanks, but it is not the title of the picker that I'm concerned with, it is the value of the variable that selected item is bound to. The error doesn'5t seem to crash the app though.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Your data from async source, then your selected item has bound to an value, due to the value is empty when you initialization, so your app is crash, am I right? If so, you can get value method do not use await, waitting for the data have poplulated it, then show the Picker.

Sign In or Register to comment.