0

我找不到获取和显示当前所选项目内容的方法,这里是 XAML:

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

和其余的代码:

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

它在“字符串文本...”行给了我这个例外:

你调用的对象是空的

4

1 回答 1

0

尝试使用下面的代码

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);
}
于 2013-06-03T13:04:44.023 回答