Wp8 -list picker -filter第二个列表选择器值基于第一个列表选择器值

时间:2014-08-31 15:15:01

标签: windows-phone-8 selecteditem listpicker

我想获得第一个listpicker的值,并根据其值过滤第二个列表选择器...任何人都可以帮我实现它吗?

2 个答案:

答案 0 :(得分:2)

试试这个..

<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>

对于第一个ListPicker(lstPicker1),您也可以从代码中动态设置项目。

我创建了这个方法来动态创建第二个ListPicker(lstPicker2)的内容。这很简单。使用类似这样的东西

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;
}

然后您使用lstPicker1中的SlectionChanged事件设置第二个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;
        }
     }
}

SelectionChanged方法中,需要If条件,因此在加载页面时不会抛出Exception

答案 1 :(得分:0)

因此,您只需使用SelectionChanged事件即可从ListPicker获取所选值的值。

How to get id values from listpicker?

Listpicker error SelectedItem must always be set to a valid value

希望它有所帮助!

相关问题