【问题标题】:How to Get Name of ListPickerItem in SelectionChanged Event如何在 SelectionChanged 事件中获取 ListPickerItem 的名称
【发布时间】:2014-11-07 20:45:49
【问题描述】:

我在后面的代码中使用基本列表动态填充 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?



                }
            }
     }

编辑**

设置项目时无法调用名称

【问题讨论】:

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


    【解决方案1】:

    使用 var 不是很好,因为 SelectedItem 是一个对象

    转换成

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 1970-01-01
      • 2011-11-30
      • 2016-08-13
      相关资源
      最近更新 更多