在listpicker wp8中显示所选值

时间:2015-03-29 08:57:37

标签: c# windows-phone-7 windows-phone-8

我想在listpicker中显示所选值。

Location": [
                {
                    "id": "208",
                    "Name": "Canberra"
                },
                {
                    "id": "209",
                    "Name": "Regional ACT"
                },
                {
                    "id": "67",
                    "Name": "NSW"
                },
                {
                    "id": "134",
                    "Name": "CBD, Inner West & Eastern Suburbs"
                }
            ],

我将此数据转换为list.Now我想将所选位置显示为listpicker

 foreach (var Seletedloc in _lst)
{
lstLocations.SelectedItem = Seletedloc ;
}

但我收到此错误.SelectedItem必须始终设置为有效值

2 个答案:

答案 0 :(得分:1)

示例XAML

   <Grid Loaded="ContentPanel_OnLoaded" x:Name="ContentPanel" Grid.Row="1"                Margin="12,0,12,0">
        <toolkit:ListPicker Name="MyListPicker"></toolkit:ListPicker>
    </Grid>

示例代码:

    private void ContentPanel_OnLoaded(object sender, RoutedEventArgs e)
    {
        var datasource = new[] {"one", "two", "three"};
        MyListPicker.ItemsSource = datasource;
        MyListPicker.SelectedItem = datasource[1];
    }

答案 1 :(得分:0)

将形成的列表分配给列表选择器的ItemsSource属性并设置所选项目。

MyListPicker.ItemsSource = LocationList;
var item = LocationList[1];
MyListPicker.SelectedItem = item;

MyListPicker.SelectedIndex = 1; 
如果SelectedItem为null或ListPicker的ItemsSource中不存在分配给SelectedItem属性的值,则ListPicker将显示此错误。