How to get the selected item from the listpicker i

2019-08-15 02:31发布

I am using this code for create listpicker in windows phone.

<StackPanel Height="148" Margin="0,100,0,0">
   <toolkit:ListPicker Grid.Row="0" FontFamily="Segoe WP Semibold" Height="176" x:Name="Additional_Time" ItemTemplate="{StaticResource PickerItemTemplate}" FullModeItemTemplate="{StaticResource PickerFullModeItemTemplate}" FullModeHeader="Cities" SelectedIndex="0" CacheMode="BitmapCache" Header="Choose Exit Time" FontSize="30" SelectionChanged="Additional_Time_SelectionChanged"/>
</StackPanel>

and use this grid resources

<Grid.Resources>
        <DataTemplate x:Name="PickerItemTemplate">
            <StackPanel Orientation="Horizontal">
                <Border Background="LightGreen" Width="34" Height="34">
                    <TextBlock Text="{Binding Country}" FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <TextBlock Text="{Binding Name}" Margin="12 0 0 0"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Name="PickerFullModeItemTemplate">
            <StackPanel Orientation="Horizontal" Margin="16 21 0 20">
                <TextBlock Text="{Binding Name}" Margin="16 0 0 0" FontSize="43" FontFamily="{StaticResource PhoneFontFamilyLight}"/>

                <TextBlock Text="{Binding Language}" Foreground="Green"/>
            </StackPanel>
        </DataTemplate>
    </Grid.Resources>

Below code for insert item into listpicker

List<Cities> source = new List<Cities>();
        //List<Items> source = new List<Items>();
        source.Add(new Cities() { Name = " 00 : 30 " });
        source.Add(new Cities() { Name = " 01 : 00 " });
        source.Add(new Cities() { Name = " 01 : 30 " });
        source.Add(new Cities() { Name = " 02 : 00 " });
        source.Add(new Cities() { Name = " 02 : 30 " });
        source.Add(new Cities() { Name = " 03 : 00 " });
        source.Add(new Cities() { Name = " 03 : 30 " });
        source.Add(new Cities() { Name = " 04 : 00 " });
        source.Add(new Cities() { Name = " 04 : 30 " });
        source.Add(new Cities() { Name = " 05 : 00 " });
        source.Add(new Cities() { Name = " 05 : 30 " });
        source.Add(new Cities() { Name = " 06 : 00 " });
        source.Add(new Cities() { Name = " 06 : 30 " });
        source.Add(new Cities() { Name = " 07 : 00 " });

        this.Additional_Time.ItemsSource = source;

My Question was how to get the selected item from this listpicker.

I tried different code for get the selected item from that listpicker it's throw null value exception or invalid cast exception.

private void Additional_Time_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var selectedItem = (sender as ListPicker).SelectedItem;
        int selindex = Additional_Time.SelectedIndex;// lp.SelectedIndex; //just for testing
        MessageBox.Show(selindex.ToString()); //just for testing
        MessageBox.Show((Additional_Time.SelectedItem as ListPicker).ToString());
    }

Please help me to solve this problem.

Thanks in advance.

1条回答
ら.Afraid
2楼-- · 2019-08-15 03:13

In your code, in the last line you are trying to cast the selected item to ListPicker type, but it should be of type Cities:

MessageBox.Show((Additional_Time.SelectedItem as Cities).Name.ToString());
查看更多
登录 后发表回答