Picker won't reset with SelectedIndex = -1 RRS feed

  • Question

  • User382926 posted

    Hi everyone,

    For a school project I'm working on a Xamarin.Forms app. It is about video games and consoles (called platforms). In one of my screens I have a ListView that shows the user's platforms and, below, I have a picker that allows the user to add a platform to his collection.

    ~~~~ ~~~~



    When an item is selected in my Picker, I need to remove it so that it cannot be selected again. It works fine, except for the last item in the picker. Since the method below is executed on SelectedIndexChanged, the picker loops through the other elements in it, until there is none left and it displays an OutofBounds exception (because the picker is then empty).

    Code of the method that adds to ListView and removes from Picker :

    private void AddToUserPlatforms(object sender, EventArgs e) { Platform p = (Platform)pckAddPlatform.SelectedItem; if (p != null) { CustomPlatform NewPlatform = new CustomPlatform(p.Id, p.Name, p.Abbreviation); ListOfPlatforms.Add(NewPlatform); AvailablePlatforms.Remove(PickedItem); } pckAddPlatform.SelectedIndex = -1; } ~~~~

    No matter what I tried I couldn't find a way to either deselect the picker's selection or simply not execute my method again.

    If anyone could help or suggest an idea it'd be greatly appreciated. Apologies if something is missing from my post, I don't post often.

    Thanks !

    Saturday, February 1, 2020 2:52 PM

All replies

  • User383174 posted

    Hi Please change this line pckAddPlatform.SelectedIndex = -1; to pckAddPlatform.SelectedItem = null;

    Sunday, February 2, 2020 5:16 AM