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


i have 2 picker. second picker dependent on first picker. both pickers bind service. using dictionary object bind data picker. i not using mvvm pattern.

  1. first service call dictionary object first picker bind.
  2. then fill first picker dictionary object. @ time second picker empty.
  3. on selectedindexchange event of first picker call service bind dictionary object of second picker.
  4. now fill values second picker dictionary object. (if picker has data put picker.items.clear())
  5. now if select value second picker , change value of first picker gives me error @ picker.items.clear()

system.argumentoutofrangeexception: index out of range. must non-negative , less size of 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 picker == null)                 return;             else             {                 var objactivitytype = sender 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 solve issue

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

but didn't find solution.

we can't clear picker if of value selected?

i don't want use bindablepicker cutom control.

i'm not sure if "clear picker" mean reset such doesn't show items selected. if that's want, can following:

  1. set selectedindex = -1. resets picker such no items selected.

  2. once set selectedindex = -1, selectedindexchange event handler called, , that's causing the

system.argumentoutofrangeexception: index out of range. must non-negative , less size of collection.

indeed yes, selectedindex -1, it's complaining index must non-negative.

  1. to avoid argumentoutofrangeexception, add if statement encloses selectedindexchange event handler handler executes
    if (selectedindexchange != -1).

in summary, this:

yourpicker.selectedindex = -1; yourpicker.selectedindexchanged += (sender, e) => {     if (yourpicker.selectedindex != -1)     {         //do stuff     } } 

Comments

Popular posts from this blog

javascript - Uncaught FirebaseError: Messaging: This method is available in a Window context -

angular - File Name and Extension not respected when sent from .NET Web API to Angular2 App -