我在代码后面使用基本List动态填充ListPicker。在SelectionChanged事件上,我需要确定用户选择的项目的名称。目前我所拥有的如下,但我无法获得该项目的名称?
XAML
<toolkit:ListPicker x:Name="lp" Visibility="Collapsed"/>
XAML.CS
//Populate the ListPicker
List<ListPickerItem> l = new List<ListPickerItem>();
l.Add(new ListPickerItem { Name = "Flip_Vertical", Content = AppResources.App_Flip_Vertical });
l.Add(new ListPickerItem { Name = "Flip_Horizontal", Content = AppResources.App_Flip_Horizontal });
l.Add(new ListPickerItem { Name = "Flip_Both", Content = AppResources.App_Flip_Both });
lp.ItemsSource = l;
lp.Visibility = Visibility.Visible;
...
void lp_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (lp.SelectedIndex != -1)
{
var item = (sender as ListPicker).SelectedItem;
if (item != null)
{
//switch () //Need to determine the Name of the currently selected item?
}
}
}
编辑**
设置项目时无法调用名称
答案 0 :(得分:0)
使用var
并不是那么好,因为SelectedItem是一个对象
将其转换为
string name = ((ListPickerItem)item).Name;