0
votes

I have a listpicker control that has a list of meals. This control is populated using following code where viewModelMeal returns a collection of type Meal

enter image description here

After that I get the Diet data from database and that also has a column name MealId. Now when I try to set the selected item of listpicker control with that mealid it gives me error that "SelectedItem must always be set to a valid value". Here is the function setting the selected item of listpicker

enter image description here

and XAML code

 <toolkit:ListPicker Name="lpMeal" Height="60" ItemsSource="{Binding}" VerticalAlignment="Bottom" Background="Black" FullModeHeader="Select your Meal" Foreground="White" ExpansionMode="FullScreenOnly" Margin="5,0">
                    <toolkit:ListPicker.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Name="listpickerStackpannel" >
                                <!--<TextBlock Text="{Binding mealId}" Visibility="Collapsed"></TextBlock>-->
                                <TextBlock Text="{Binding mealName}" TextAlignment="Center" FontFamily="Times New Roman" FontSize="30"></TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </toolkit:ListPicker.ItemTemplate>
                    <toolkit:ListPicker.FullModeItemTemplate>
                        <DataTemplate>
                            <StackPanel Name="listpickerStackpannel" Margin="10">
                                <!--<TextBlock Text="{Binding mealId}" Visibility="Collapsed"></TextBlock>-->
                                <TextBlock Text="{Binding mealName}" TextAlignment="Center" FontFamily="Times New Roman" FontSize="30"></TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </toolkit:ListPicker.FullModeItemTemplate>
                </toolkit:ListPicker>
1
You can only set it to an item (model) of the ItemsSource. Not an arbitrary number.Chubosaurus Software

1 Answers

0
votes

To elaborate on Chubosaurus Software's comment.

You need to set the SelectedItem to a value that is in the list that the lpMeal ItemsSource is bound to. In your case:

lpMeal.SelectedItem = DietViewModel.GetDefault().GetItem(ID);

Therefore you are setting the SelectedItem to a member of the list in it's ItemsSource.