[英]How to Get the Selected Item of a ListPicker
我想確定當前在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?
}
var item = (sender as ListPicker).SelectedItem;
可能是這樣的。
Theme selectedObj = ThemeListPicker.SelectedItem as Theme;
這是您可以幫助您的解決方案。
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.