windows-phone-8selecteditemlistpicker

Wp8 -list picker -filter second list picker values based on first list picker value


I would like to get the value of a first listpicker and based on the its value filter the second list picker...Can anyone help me how to achieve it?


Solution

  • try this..

    <StackPanel>
        <toolkit:ListPicker Name="lstPicker1" SelectionChanged="lstPicker1_SelectionChanged">
            <sys:String>Option 1</sys:String>
            <sys:String>Option 2</sys:String>
            <sys:String>Option 3</sys:String>
            <sys:String>Option 4</sys:String>
            <sys:String>Option 5</sys:String>
        </toolkit:ListPicker>
    
        <toolkit:ListPicker Name="lstPicker2">
        </toolkit:ListPicker>
    </StackPanel>
    

    Here as for the first ListPicker (lstPicker1) you can also set the items dynamically from the code as well.

    I have created this method to dynamically create the content of the second ListPicker (lstPicker2). This is simple. Use something like this for your use

    private List<string> CreateList(int opt)
    {
        List<string> strLst = new List<string>();
        for (int i = 1; i < 6; i++)
        {
            string str = string.Format("Sub-option {0}.{1}", opt, i);
            strLst.Add(str);
        }
        return strLst;
    }
    

    Then you use the SlectionChanged event from your lstPicker1 to set the items in the second ListPicker

    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;
                case 2:
                    lstPicker2.ItemsSource = CreateList(3);
                    break;
                case 3:
                    lstPicker2.ItemsSource = CreateList(4);
                    break;
                case 4:
                    lstPicker2.ItemsSource = CreateList(5);
                    break;
                default:
                    break;
            }
         }
    }
    

    Here in the SelectionChanged method the If condition is need so it won't throw an Exception when the page is loading.