문제

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?
    }
도움이 되었습니까?

해결책

var item = (sender as ListPicker).SelectedItem;

다른 팁

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;
           }
        }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top