简体   繁体   中英

Show selected value in listpicker wp8

I want to show selected value in listpicker.

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

I'm convert this data to list.Now I want to show selected location to listpicker

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

But I get this error .SelectedItem must always be set to a valid value

Sample XAML

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

Sample Code:

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

Assign the list formed to the ItemsSource property of list picker and set the selected item.

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

or

MyListPicker.SelectedIndex = 1; 

ListPicker will show this error if the SelectedItem is null or if the value assigned to the SelectedItem property is not present in the ItemsSource of ListPicker.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM