选择项目时,列表选择器会出错

时间:2014-12-16 03:32:32

标签: c# xaml windows-phone-8 windows-phone-8.1

我有一个listpicker控件,里面有一份餐食清单。使用以下代码填充此控件,其中viewModelMeal返回Meal

类型的集合

enter image description here

之后我从数据库中获取Diet数据,并且还有一个名称为MealId。现在,当我尝试使用该feedid设置listpicker控件的所选项时,它给出了错误“SelectedItem必须始终设置为有效值”。以下是设置listpicker所选项目的功能

enter image description here

和XAML代码

 <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 个答案:

答案 0 :(得分:0)

详细说明Chubosaurus Software的评论。

您需要将SelectedItem设置为lpMeal ItemsSource绑定到的列表中的值。在你的情况下:

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

因此,您将SelectedItem设置为其所在ItemsSource中列表的成员。