简体   繁体   中英

How to clear picker if It is selected in xamarin forms?

I have two picker. second picker is dependent on first picker. Both pickers are bind from Service. I am using dictionary object to bind data to picker. I am not using MVVM pattern.

  1. First service call where dictionary object for first picker is bind.
  2. then fill first picker from that dictionary object. At that time Second picker is empty.
  3. On selectedIndexChange event of first picker call service to bind dictionary object of second picker.
  4. Now Fill values to second picker from dictionary object. (If already picker has data then put Picker.Items.clear() )
  5. Now If I select some value from second picker and change value of first picker then It gives me error at Picker.Items.clear()

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index

Global Declaration :

Dictionary<string, string> DicObjActivityType;

Dictionary<string, string> DicObjSelfActivity;

First Picker selectedIndexChange event

private async void PckrActivityType_SelectedIndexChanged(object sender, EventArgs e)
{
    if (sender as Picker == null)
        return;
    else
    {
        var objActivityType = sender as Picker;
        var Key = DicObjActivityType.FirstOrDefault(x => x.Value == objActivityType.Items[objActivityType.SelectedIndex]).Key;
        PickedActivityType = Key;
        if (Key != "0")
        {
            PckrSelfActivity.IsEnabled = true;
            await CallGetWebService_SelfActivity(Key);
            if (PckrSelfActivity.IsEnabled == true)
            {
                PckrSelfActivity.Items.Clear();
                foreach (string items in DicObjSelfActivity.Values)
                {
                    PckrSelfActivity.Items.Add(items);
                }
            }
        }
        else
        {
            PckrSelfActivity.IsEnabled = false;
        }
    }
}

Call Service of second picker

private async Task CallGetWebService_SelfActivity(string strkey)
{
    try
    {
        var response = await GetResponseFromWebService.GetResponse<ServiceClasses.RootObject_LstListComboData>(ServiceURL.GetSelfActivity + "ActivityTypeCd=" + strkey);

        if (response.Flag == true)
        {
            DicObjSelfActivity = new Dictionary<string, string>();
            DicObjSelfActivity.Add("0", "--Select--");
            if (response.lstListComboData != null)
            {
                foreach (ServiceClasses.LstListComboData Items in response.lstListComboData)
                {
                    DicObjSelfActivity.Add(Items.Value, Items.Text);
                }
            }
        }
        else
        {
            PckrSelfActivity.IsEnabled = false;
        }
    }
    catch (Exception e)
    {
        await DisplayAlert(AppResources.LError, AppResources.LConnectionError, "OK");
    }
}

I refer following link to solve this issue

https://forums.xamarin.com/discussion/55922/picker-clear-system-argumentoutofrangeexception

but didn't find solution.

We can't clear picker if any of the value is selected?

I don't want to use BindablePicker cutom control.

I'm not sure if by "clear the picker" you mean reset it such that it doesn't show any items as selected. But if that's what you want, then you can do the following:

  1. Set SelectedIndex = -1 . This resets the picker such that no items are selected.

  2. Once you set SelectedIndex = -1 , the selectedIndexChange event handler is called, and that's causing the

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

Indeed yes, the SelectedIndex is -1 , so it's complaining that the index must be non-negative.

  1. To avoid the ArgumentOutOfRangeException, add an if statement that encloses your SelectedIndexChange event handler so that the handler only executes
    if (SelectedIndexChange != -1) .

In summary, do this:

YourPicker.SelectedIndex = -1;
YourPicker.SelectedIndexChanged += (sender, e) =>
{
    if (YourPicker.SelectedIndex != -1)
    {
        //Do your stuff
    }
}

I think you receive this error because you clean all items in the picker. I had the same error but I'm using now MVVM, it's more simple and you can manage this kind of thing better.

Actually i do the following and it is working fine for me:

//step 1: re-select index to -1
YourPicker.SelectedIndex = -1;
//step 2: clear all items
YourPicker.Items.Clear();
//refill the picker 
foreach (var item in subAccountStatus)
{
    YourPicker.Items.Add(item.AccountStatus);                  
}
//re-selected index to first item
YourPicker.SelectedIndex = 0;

Now that the Picker class uses bindings, you can bind a List of strings to the picker using the ItemSource attribute. What I did to fix this issue in my apps is I created a helper method called GeneratePickerSource()

This is my Picker in XAML:

<Picker x:Name="myPicker" Title="Placeholder"/>

Then my GeneratePickerSource() method:

List<string> pickerStrings = new List<string>();

//Add your picker options to the list

myPicker.ItemSource = pickerStrings;

By using this method, whenever you call GeneratePickerSource() , the Picker will be reset and there will be no selected item (for myPicker , it will show "Placeholder" since that is its Title ).

This solution works well if your list is short, which is most likely the case. But this may be slow if you have a very, very large list you need to generate each time.

In your case, you can do the same thing with the Dictionary as I did with the List , simply create a new Dictionary , fill it, and set it as the ItemSource .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM