我想确定当前在ListPicker中选择的项目的名称。我不确定在SelectionChanged事件中要做什么来获取项目的名称。
XAML
<Grid.Resources>
<DataTemplate x:Name="PickerItemTemplate">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</Grid.Resources>
<toolkit:ListPicker x:Name="ThemeListPicker" Header="{Binding Path=LocalizedResources.SettingsPage_ThemeListPicker_Header, Source={StaticResource LocalizedStrings}}"
ItemTemplate="{StaticResource PickerItemTemplate}"
SelectionChanged="ThemeListPicker_SelectionChanged"/>
XAML.CS
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
themeList = new List<Theme>();
themeList.Add(new Theme() { Name = "light" });
themeList.Add(new Theme() { Name = "dark" });
ThemeListPicker.ItemsSource = themeList;
}
private void ThemeListPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//get the name of the current item in the listpicker?
}
答案 0 :(得分:6)
var item = (sender as ListPicker).SelectedItem;
答案 1 :(得分:3)
可能是这样的。
Theme selectedObj = ThemeListPicker.SelectedItem as Theme;
答案 2 :(得分:3)
这是您可以帮助您的解决方案。
private void ThemeListPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//get the name of the current item in the listpicker?
string currentItemName =string.Empty;
Theme theme= (sender as ListPicker).SelectedItem as Theme;
if(theme!=null)
{
currentItemName = item.Name;
}
}