Search code examples
c#xamlwindows-phone-8listpicker

How to Get Name of ListPickerItem in SelectionChanged Event


I am dynamically populating a ListPicker using a basic List in code behind. On the SelectionChanged event I need to determine the name of the item that the user selected. Currently what I have is as follows, but I cannot get the Name of the item?

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?



                }
            }
     }

EDIT**

Cannot call Name when setting item

enter image description here


Solution

  • Using var is not that great since SelectedItem is an object

    Convert it to

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