XAML
<DataTemplate x:Name="PickTmplItemTipo">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding tipo}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Name="PickTmplFullTipo">
<StackPanel Orientation="Horizontal" Margin="0,25,0,0">
<TextBlock Name="lblTipo" Width="350" Text="{Binding tipo}" FontFamily="{StaticResource PhoneFontFamilyLight}" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeExtraLarge}" />
</StackPanel>
</DataTemplate>
<toolkit:ListPicker
Grid.Row="0"
ItemsSource="{Binding}" Margin="21,0,22,0"
Header="{Binding Source={StaticResource LocalizedStrings}, Path=Localizedresources.strTipoUni}"
FullModeHeader="{Binding Source={StaticResource LocalizedStrings}, Path=Localizedresources.strTipoUni}"
FullModeItemTemplate="{Binding Source={StaticResource PickTmplFullTipo}}"
ItemTemplate="{Binding Source={StaticResource PickTmplItemTipo}}"
Name="lPickTipo"
TabIndex="0"
Height="98"
VerticalAlignment="Top"
ExpansionMode="FullScreenOnly"
Tap="lPickTipo_Tap"
SelectionChanged="lPickTipo_SelectionChanged" />
C#
填写列表选择器:
List<tipos> _lstTipos { get; set; }
private void cargaLista()
{
using (serviciosDBDataContext miDataContext = new serviciosDBDataContext(conn))
{
_lstTipos = miDataContext.tipos.ToList();
}
this.lPickTipo.ItemsSource = _lstTipos;
}
设置所选项目:
如果我尝试此操作,则返回此错误“必须始终将 SelectedItem 设置为有效值。”
this.lPickTipo.SelectedItem = myStringValue;
如果我尝试下一件事,则返回 null 错误:
this.lPickTipo.SelectedItem = lPickTipo.Items.First(x => (x as ListPickerItem).Content.ToString() == myStringValue);
但我不能通过 selectindex 设置,因为我不知道哪个索引等效