Skip to content

Picker Attribute "SelectedIndex" Not being respected on page load on Android? #318

@japarson

Description

@japarson

Description

I have a Picker that is attached to an enum for a "list type" property, with a "SelectedIndex=0" attribute on the picker, which I believe means that I want that index of the enum to be selected first, as a 'default'. This works great on the windows app, but when running on Android, the picker control is empty. Then, I'm able to force something into the control by manually changing the "selected index" in the .xaml from say, 0 to 1, which I think then triggers the event, and updates in the app. But obviously since I think the point of SelectedIndex is to default something in, then, well, it should default something in.

I found a workaround for this by grabbing the picker control in the constructor in the code-behind for the page, and setting it's SelectedIndex property there, which fixes it. But I presume that the xaml property should work without needing to do that.

Steps to Reproduce

  1. Use Picker Control in a ContentPage
  2. Set the "SelectedIndex="0"" value on the Picker
  3. See that on that page loading, nothing is populated on Android, but is on the Windows App

Did you find any workaround?

Yes, as described above - "I found a workaround for this by grabbing the picker control in the constructor in the code-behind for the page, and setting it's SelectedIndex property there, which fixes it."

Investigation

Investigated this for a few hours. It seems that in XamlNode.cs the properties of Picker have SelectedIndex above the Binding PickerItems.

It first then iterates into SelectedIndex, eventually it comes in BindableObject.cs in SetValueCore, where property.CoerceValue is executed against the selectedIndex from the xaml (1 in my example). It is coerced to -1 because there are no items yet, and after that it is not loaded from the XamlNode again.

The next property after SelectedIndex is the PickerItems Binding.

I have yet to figure out what determines the order in which properties are listed, so that I can change the behavior. As seen in the screenshot other properties x:name and textColor are applied after the Binding PickerItems.

Files to Investigate

  • src/Controls/src/Core/Picker.cs
  • XamlNode.cs
Checklist
  • Modify src/Controls/src/Core/Picker/Picker.cs0df0b64 Edit
  • Running GitHub Actions for src/Controls/src/Core/Picker/Picker.csEdit
  • Modify src/Controls/src/Xaml/XamlNode.csc84fc59 Edit
  • Running GitHub Actions for src/Controls/src/Xaml/XamlNode.csEdit

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions