我想获得第一个listpicker的值,并根据其值过滤第二个列表选择器...任何人都可以帮我实现它吗?
答案 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
希望它有所帮助!