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.
- first service call dictionary object first picker bind.
- then fill first picker dictionary object. @ time second picker empty.
- on selectedindexchange event of first picker call service bind dictionary object of second picker.
- now fill values second picker dictionary object. (if picker has data put picker.items.clear())
- 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:
set
selectedindex = -1
. resets picker such no items selected.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.
- to avoid argumentoutofrangeexception, add
if
statement enclosesselectedindexchange
event handler handler executesif (selectedindexchange != -1)
.
in summary, this:
yourpicker.selectedindex = -1; yourpicker.selectedindexchanged += (sender, e) => { if (yourpicker.selectedindex != -1) { //do stuff } }
Comments
Post a Comment