I would like to determine the name of the item that is currently selected in a ListPicker. I am not sure what to do in the SelectionChanged event to get the name of the item.
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?
}
Matthew
var item = (sender as ListPicker).SelectedItem;
A.K.
May be like this.
Theme selectedObj = ThemeListPicker.SelectedItem as Theme;
Here is the solution this ma be help you.
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;
}
}
来源:https://stackoverflow.com/questions/20591841/how-to-get-the-selected-item-of-a-listpicker