Forum Xamarin.Forms

Picker won't reset with SelectedIndex = -1

xNovxNov Member ✭✭

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.

<ListView x:Name="lv_Platforms" x:FieldModifier="public" ItemsSource="{Binding ListOfPlatforms}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <local:CustomViewCell SelectedBackgroundColor="LightSkyBlue">
                            <StackLayout Orientation="Horizontal">
                                <Image Source="{Binding PlatformLogo}"/>
                                <StackLayout Orientation="Horizontal">
                                    <Label Text="{Binding Name}" VerticalTextAlignment="Center" HorizontalOptions="Start" Padding="5,0,0,0" FontAttributes="Bold"/>
                                    <ImageButton Source="icon_delete.png" VerticalOptions="Center" Aspect="AspectFit" HorizontalOptions="End" Clicked="RemovePlatform" ></ImageButton>
                                </StackLayout>
                            </StackLayout>
                        </local:CustomViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            <Picker x:Name="pck_AddPlatform" ItemsSource="{Binding AvailablePlatforms}" ItemDisplayBinding="{Binding Name}" SelectedIndexChanged="AddToUserPlatforms" SelectedItem="{Binding PickedItem, Mode=TwoWay}"></Picker>

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)pck_AddPlatform.SelectedItem;
            if (p != null)
            {
                CustomPlatform NewPlatform = new CustomPlatform(p.Id, p.Name, p.Abbreviation);
                ListOfPlatforms.Add(NewPlatform);
                AvailablePlatforms.Remove(PickedItem);
            }
            pck_AddPlatform.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 !

Answers

  • mjdevelopermjdeveloper Member ✭✭✭
    edited February 2020

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

Sign In or Register to comment.