如何获取ListPicker的选定项目

时间:2013-12-15 06:22:17

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

我想确定当前在ListPicker中选择的项目的名称。我不确定在SelectionChanged事件中要做什么来获取项目的名称。

XAML

<Grid.Resources>
        <DataTemplate x:Name="PickerItemTemplate">
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </Grid.Resources>

<toolkit:ListPicker x:Name="ThemeListPicker" Header="{Binding Path=LocalizedResources.SettingsPage_ThemeListPicker_Header, Source={StaticResource LocalizedStrings}}"
                                        ItemTemplate="{StaticResource PickerItemTemplate}"
                                        SelectionChanged="ThemeListPicker_SelectionChanged"/>

XAML.CS

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        themeList = new List<Theme>();
        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?
    }

3 个答案:

答案 0 :(得分:6)

var item = (sender as ListPicker).SelectedItem;

答案 1 :(得分:3)

可能是这样的。

Theme selectedObj = ThemeListPicker.SelectedItem as Theme;

答案 2 :(得分:3)

这是您可以帮助您的解决方案。

 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;
           }
        }