Question

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?
    }
Was it helpful?

Solution

var item = (sender as ListPicker).SelectedItem;

OTHER TIPS

May be like this.

Theme selectedObj = ThemeListPicker.SelectedItem as Theme;

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;
           }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top