2
votes

Am new to Wp7, developing the app with ListPicker and used SelectionChanged event to get selected data form listPicker but am having the problem with SelectionChanged event when am using this and getting NullReferenceException but when i used same code in button_Click that works perfectly and am getting the selected text

my c# code is :

 private void listPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ListPickerItem lpi = (ListPickerItem)listPicker1.SelectedItem;//this code is working in click event
        MessageBox.Show(lpi.Content.ToString());
    }

my Xaml code is

<toolkit:ListPicker x:Name="listPicker1" Grid.Row="0" ExpansionMode="ExpansionAllowed" SelectionChanged="listPickerCountryLogin_SelectionChanged" HorizontalAlignment="Left" Margin="14,43,0,0" VerticalAlignment="Top" Width="436" FullModeHeader="Select Country" Background="White" BorderBrush="White">
            <toolkit:ListPicker.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Country}" Width="250" />
                    </StackPanel>
                </DataTemplate>
            </toolkit:ListPicker.ItemTemplate>
            <toolkit:ListPicker.FullModeItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Country}" Width="300" Margin="0,0,0,20" FontSize="44"/>
                    </StackPanel>
                </DataTemplate>
            </toolkit:ListPicker.FullModeItemTemplate>
        </toolkit:ListPicker>

but i want to get the text form SelectionChanged event?

how to achive this :)

thanks in advance

4

4 Answers

4
votes

am also ran into same problem am also getting NullReferenceException

Try this works fine for me

1) If you are using static ListPickerItems means without DataBinding use this

private void listPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ListPickerItem lpi = (sender as ListPicker).SelectedItem as ListPickerItem;
    MessageBox.Show("selected item is : " + lpi.Content);
}

2) Try this if You are using DataBinding to dispaly the listPickerItems

 private void listPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
     Countries item = (sender as ListPicker).SelectedItem as Countries;
     MessageBox.Show("Selected Item is : " + item.Country);
}

here am assuming that you prepared a class Countries with country property for taht you need to typeCast to selected item to Countries class then only you get the result

1
votes

I had this same problem when using SelectionChanged event in my listpicker, here is what I had:

// My listpicker is LpBluetoothPaired

 private void LpBluetoothPaired_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

         rcvData.Text = LpBluetoothPaired.SelectedItem.ToString();

    }

But when opening the app it had an exception, so I fixed it:

 private void LpBluetoothPaired_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

       if (LpBluetoothPaired.SelectedItem != null) {
            rcvData.Text = LpBluetoothPaired.SelectedItem.ToString();
        }
    }

it looks like the event is called when the application is opening but at that time there is still no selectedItem, so in order to avoid the exception and only fulfill the rcvData textBox I check if it isn''t null

0
votes

Sometimes SelectionChanged event fires, when setting the ItemsSource from code behind. So, in such case the selectedItem may be null.

Hence, add this line in your SelectionChanged code and try.

private void listPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if(listPicker1.SelectedIndex == -1) //otherwise (listPicker1.SelectedItem == null) also works
    return;
    ListPickerItem lpi = (ListPickerItem)listPicker1.SelectedItem;//this code is working in click event
    MessageBox.Show(lpi.Content.ToString());
}

If still problem persists, place a break point in the SelectionChanged handler, and observe the values

0
votes

When data is loading to the listpicker it fires selectionchanged event.So for this put your code in Try-catch and For selectionchanged write this :

private void listPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
     try
     {
        Countries SelectedCountries =e.AddedItems[0] as Countries;
        String SelectedCountry = SelectedCountries.Country;
     }
     catch
     {
     }
}