如何在SelectionChanged事件中获取ListPickerItem的名称

时间:2014-11-07 20:45:50

标签: c# xaml windows-phone-8 listpicker

我在代码后面使用基本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?



                }
            }
     }

编辑**

设置项目时无法调用名称

enter image description here

1 个答案:

答案 0 :(得分:0)

使用var并不是那么好,因为SelectedItem是一个对象

将其转换为

string name = ((ListPickerItem)item).Name;