如何获取ListPicker的选定项目
我想确定当前在ListPicker中选择的项目的名称。 我不确定在SelectionChanged事件中要做什么来获取项目的名称。
XAML
XAML.CS
protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); themeList = new List(); 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; } }