【问题标题】:Show selected value in listpicker wp8在listpicker wp8中显示选定的值
【发布时间】:2015-03-29 08:57:36
【问题描述】:

我想在列表选择器中显示选定的值。

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

我正在将此数据转换为列表。现在我想向列表选择器显示所选位置

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

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

【问题讨论】:

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


    【解决方案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];
        }
    

    【讨论】:

      【解决方案2】:

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

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

      MyListPicker.SelectedIndex = 1; 
      

      如果 SelectedItem 为 null 或分配给 SelectedItem 属性的值在 ListPicker 的 ItemsSource 中不存在,ListPicker 将显示此错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多