0
votes

I can't find a way to get and show the content of the current selected item, here is the XAML:

<toolkit:ListPicker Name="lp" Header="Network" SelectionChanged="selectionChanged">
            <toolkit:ListPickerItem Content="aaa" />
            <toolkit:ListPickerItem Content="bbb" />
</toolkit:ListPicker>

and the rest of the code:

private void selectionChanged(object sender, SelectionChangedEventArgs e)
    {
        try
        {
            if (e.RemovedItems != null && e.RemovedItems.Count > 0)
            {
                if (this.lp.SelectedItem != null)
                {
                    var selectedItem = (sender as ListPicker).SelectedItem;
                    int selindex = lp.SelectedIndex; //just for testing
                    MessageBox.Show(selindex.ToString()); //just for testing
                    string text = (lp.SelectedItem as ListBoxItem).Content.ToString();
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

It gives me this exception at "string text..." line:

Object reference not set to an instance of an object

1
Which line do you get the exception on? You may need to take away your catch block to see this.JMK
It gives me the exception at the "string text..." line. The rest is working fine, it gives me the current index.sparcopt
I think this link might answer your question. stackoverflow.com/questions/15325243/…YnotDraw

1 Answers

0
votes

Try using code below

try
{
   if (e.RemovedItems != null && e.RemovedItems.Count > 0)
   {
        if (this.mode.SelectedItem != null)
        {
             var selectedItem = (sender as ListPicker).SelectedItem as ListPickerItem;
             int selindex = mode.SelectedIndex; //just for testing
             MessageBox.Show(selindex.ToString()); //just for testing
             string text = (selectedItem as ListPickerItem).Content.ToString();
        }
   }
}
catch (Exception ex)
{
       MessageBox.Show(ex.Message);
}