繁体   English   中英

如何获取ListPicker的选定项目

[英]How to Get the Selected Item of a 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?
    }
var item = (sender as ListPicker).SelectedItem;

可能是这样的。

Theme selectedObj = ThemeListPicker.SelectedItem as Theme;

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

 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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM