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

Marc George 171 Reputation points
2023-04-26T15:36:40.4633333+00:00

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.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,384 questions
{count} votes

Accepted answer
  1. Rob Caplan - MSFT 5,442 Reputation points Microsoft Employee
    2023-05-01T22:05:43.28+00:00

    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.

    --Rob


1 additional answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 41,601 Reputation points Microsoft Vendor
    2023-05-01T06:07:21.9366667+00:00

    Hello,

    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.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.