wpfxamlwindows-phone-8listpicker

listpicker-populate lstPicker2 based on the selection on lstPicker1


i am implementing listpicker in my project. The scenario is when i select an item from lstPicker1, the lstPicker2 items should be changed based on the selection

Here is the 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 its going to below code,

private List<string[]> CreateList(int opt)
    {
   List<string> strLst = new List<string>();
         if (opt==1)
              {
                  String[] str = new String[] { "Option1", "Option2" };
                   for (int i = 0; i < 2; i++)
                      {
                         String str1 = str[i];
                         strLst.Add(str1);                               
                      }
                        return strLst;

            }

       if (opt==2)
              {
                  String[] str = new String[] { "Option3", "Option4", "Option5" };
                   for (int i = 0; i < 3; i++)
                      {
                         String str1 = str[i];
                         strLst.Add(str1);                               
                      }
                        return strLst;

            }
        }          

But unfortunately its throwing out error @ return strLst; (Error: cannot implicitly convert string to string[])

Please let me know, what i missed on the above coding or if there is something wrong in my approach.


Solution

  • The problem is that your CreateList return type is List<string[]> which basically is a list of arrays of string values while you are trying to return a list of strings.

    Just change the signature of the CreateList method to the following:

    private List<string> CreateList(int opt)
    

    Remarks on the code:

    I actually do not see any reason why are you creating an array of strings and the you are filling your list in the cycle. Just in case - you can create a list of items in the same way as you are creating an array.

    You may want to read this article on msdn, the part on Collection initializers

    Below is alternative implementation of the CreateList method which takes much less lines of code:

    private List<string> CreateList(int opt)
    {
         if (opt==1)
         {
              return new List<string> { "Option1", "Option2" }
         }
    
         if (opt==2)
         {
              return new List<string> { "Option3", "Option4", "Option5" };
         }
    
         return new List<string>();
    }