Thursday, 15 July 2010

wpf - listpicker-populate lstPicker2 based on the selection on lstPicker1 -



wpf - listpicker-populate lstPicker2 based on the selection on lstPicker1 -

i implementing listpicker in project. scenario when select item lstpicker1, lstpicker2 items should changed based on selection

here lstpicker1 selection changed event:

private void lstpicker1_selectionchanged(object sender, selectionchangedeventargs e) { if (lstpicker1 != null) { switch (lstpicker1.selectedindex) { case 0: lstpicker2.itemssource = createlist(1); break; case 1: lstpicker2.itemssource = createlist(2); break; default: break; } } }

and going below code,

private list<string[]> createlist(int opt) { list<string> strlst = new list<string>(); if (opt==1) { string[] str = new string[] { "option1", "option2" }; (int = 0; < 2; i++) { string str1 = str[i]; strlst.add(str1); } homecoming strlst; } if (opt==2) { string[] str = new string[] { "option3", "option4", "option5" }; (int = 0; < 3; i++) { string str1 = str[i]; strlst.add(str1); } homecoming strlst; } }

but unfortunately throwing out error @ return strlst; (error: cannot implicitly convert string string[])

please allow me know, missed on above coding or if there wrong in approach.

the problem createlist homecoming type list<string[]> list of arrays of string values while trying homecoming list of strings.

just alter signature of createlist method following:

private list<string> createlist(int opt)

remarks on code:

i not see reason why creating array of strings , filling list in cycle. in case - can create list of items in same way creating array.

you may want read article on msdn, part on collection initializers

below alternative implementation of createlist method takes much less lines of code:

private list<string> createlist(int opt) { if (opt==1) { homecoming new list<string> { "option1", "option2" } } if (opt==2) { homecoming new list<string> { "option3", "option4", "option5" }; } homecoming new list<string>(); }

wpf xaml windows-phone-8 listpicker

No comments:

Post a Comment