我正在使用此代码在 Windows Phone 中创建列表选择器。
<StackPanel Height="148" Margin="0,100,0,0">
<toolkit:ListPicker Grid.Row="0" FontFamily="Segoe WP Semibold" Height="176" x:Name="Additional_Time" ItemTemplate="{StaticResource PickerItemTemplate}" FullModeItemTemplate="{StaticResource PickerFullModeItemTemplate}" FullModeHeader="Cities" SelectedIndex="0" CacheMode="BitmapCache" Header="Choose Exit Time" FontSize="30" SelectionChanged="Additional_Time_SelectionChanged"/>
</StackPanel>
并使用此网格资源
<Grid.Resources>
<DataTemplate x:Name="PickerItemTemplate">
<StackPanel Orientation="Horizontal">
<Border Background="LightGreen" Width="34" Height="34">
<TextBlock Text="{Binding Country}" FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
<TextBlock Text="{Binding Name}" Margin="12 0 0 0"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Name="PickerFullModeItemTemplate">
<StackPanel Orientation="Horizontal" Margin="16 21 0 20">
<TextBlock Text="{Binding Name}" Margin="16 0 0 0" FontSize="43" FontFamily="{StaticResource PhoneFontFamilyLight}"/>
<TextBlock Text="{Binding Language}" Foreground="Green"/>
</StackPanel>
</DataTemplate>
</Grid.Resources>
下面的代码用于将项目插入列表选择器
List<Cities> source = new List<Cities>();
//List<Items> source = new List<Items>();
source.Add(new Cities() { Name = " 00 : 30 " });
source.Add(new Cities() { Name = " 01 : 00 " });
source.Add(new Cities() { Name = " 01 : 30 " });
source.Add(new Cities() { Name = " 02 : 00 " });
source.Add(new Cities() { Name = " 02 : 30 " });
source.Add(new Cities() { Name = " 03 : 00 " });
source.Add(new Cities() { Name = " 03 : 30 " });
source.Add(new Cities() { Name = " 04 : 00 " });
source.Add(new Cities() { Name = " 04 : 30 " });
source.Add(new Cities() { Name = " 05 : 00 " });
source.Add(new Cities() { Name = " 05 : 30 " });
source.Add(new Cities() { Name = " 06 : 00 " });
source.Add(new Cities() { Name = " 06 : 30 " });
source.Add(new Cities() { Name = " 07 : 00 " });
this.Additional_Time.ItemsSource = source;
我的问题是如何从此列表选择器中获取所选项目。
我尝试了不同的代码来从该列表选择器中获取所选项目,它抛出空值异常或无效转换异常。
private void Additional_Time_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedItem = (sender as ListPicker).SelectedItem;
int selindex = Additional_Time.SelectedIndex;// lp.SelectedIndex; //just for testing
MessageBox.Show(selindex.ToString()); //just for testing
MessageBox.Show((Additional_Time.SelectedItem as ListPicker).ToString());
}
请帮我解决这个问题。
提前致谢。