How to get the selected item from the listpicker in windows phone 8?

扶醉桌前 提交于 2019-12-11 10:33:19

问题


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:


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());


来源:https://stackoverflow.com/questions/17399793/how-to-get-the-selected-item-from-the-listpicker-in-windows-phone-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!