How to Get the Selected Item of a ListPicker

2019-05-10 00:50发布

I would like to determine the name of the item that is currently selected in a ListPicker. I am not sure what to do in the SelectionChanged event to get the name of the item.

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条回答
贪生不怕死
2楼-- · 2019-05-10 00:53

Here is the solution this ma be help you.

 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;
           }
        }
查看更多
萌系小妹纸
3楼-- · 2019-05-10 01:11
var item = (sender as ListPicker).SelectedItem;
查看更多
时光不老,我们不散
4楼-- · 2019-05-10 01:11

May be like this.

Theme selectedObj = ThemeListPicker.SelectedItem as Theme;
查看更多
登录 后发表回答