What event is called when Index #0 is selected in a MAUI picker?

Marc George 171 Reputation points

What event happens upon the immediate selection of index #0 in a MAUI picker resulting in no index change? I have had to add an unnecessary "Select a.." to be the Index #0 to force a selection.

[moved in from comments]

VS Version 17.6.0 Preview 5.0

[moved in from comments]

I expect the handler function to be called which is if list entry #2 or greater is selected; the behavior is that the picker just disappears. There is no error encountered. The page buttons on the screen remain active and allow the app to perform their actions.

[moved in from comments]
I don't want to be able to select index #0 as I am using it as "directions" to increase UI effectiveness. However, if it is pressed, the list disappears, it becomes the selection and it goes somewhere but not to the handler called for other selections. Hence the question, "What event is called?" I want to have a totally different UI interaction, perhaps a popup, hopefully without the list disappearing, instructing selection completion.

A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,137 questions
{count} votes

Accepted answer
  1. Rob Caplan - MSFT 5,387 Reputation points Microsoft Employee

    Hi Marc,

    If you just want an instructional title such as "Select a Monkey" you can set it as the Title of the Picker:

    <Picker Title="Select a monkey"
            ItemsSource="{Binding Monkeys}"
            ItemDisplayBinding="{Binding Name}" />

    See the Picker documentation for more details.

    If you want your help option to be more complicated you'll probably be best off putting it next to the Picker rather than inside the picker.

    The Picker control doesn't provide a way to stop a selection from being made. It assumes that all actual entries are equally valid and can be picked.

    The SelectedIndexChanged notification will fire for all Index changes, including index 0, and you can take different actions (like showing your help text) based on the chosen index, but you can't prevent the selection popup from closing when the index is changed.

    On some platforms this can be overridden at the native level to make read-only picker items, but Android will be tricky as the picker is built from smaller controls rather than based on a fully formed native picker.

    There isn't any special casing for index 0. At the MAUI level it should trigger the same SelectedIndexChanged handler as selecting the other indexes, and I'm unable to reproduce the behaviour of the selection going elsewhere. Note that SelectedIndexChanged will fire only if the index changes, so if Index 0 is already chosen and the user choose it again then there won't be a change notification. This is the same for all indices.

    If you need more help on this can you please edit your post to include more details:

    • Versions of Maui and target platforms, especially if your behaviour differs by platform
    • Minimal Xaml & code needed to demonstrate the problem (not a full project)
    • Exact repro steps
    • Actual behaviour
    • Desired behaviour (and how it differs from Actual)

    I moved some of the info you provided in comments into the question for you so it's more clear.


1 additional answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 24,356 Reputation points Microsoft Vendor


    Thanks for your feedback.

    I have tested your project, and noticed that you set the element of index 0 as the default element: PickerControl.SelectedIndex = 0;.

    Therefore, selecting an element with index 0 again after picker creation is complete causes the SelectedIndexChanged event to not fire.

    SelectedIndex of type int, the index of the selected item, which defaults to -1.

    According to the description in the Picker documentation, you could delete PickerControl.SelectedIndex = 0; to trigger the event when an element with index 0 is selected.

    Best Regards,

    Alec Liu.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.