0
votes

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

2

2 Answers

1
votes

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];
    }
0
votes

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.