简体   繁体   English

C#WP8从listPicker获取所选内容

[英]C# WP8 Get selected content from listPicker

I can't find a way to get and show the content of the current selected item, here is the XAML: 我找不到一种获取和显示当前所选项目内容的方法,这是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 你调用的对象是空的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM