Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列表框选择事件错误_C#_.net_Wpf_Nullreferenceexception - Fatal编程技术网

C# 列表框选择事件错误

C# 列表框选择事件错误,c#,.net,wpf,nullreferenceexception,C#,.net,Wpf,Nullreferenceexception,我正在使用列表框从列表框中选择一个项目,但当我选择它时,它会生成一个秘密异常。我的代码如下: ` 无效打印文本(对象发送者,选择ChangedEventArgs args) { ListBoxItem lbi=((发件人作为ListBox)。选择EdItem作为ListBoxItem); 字符串a=lbi.Content.ToString(); Window1 neww=null; neww=新窗口1(); neww.Show(); } `我不知道我在哪里做错了,请引导我。我附上它的图像,让

我正在使用列表框从列表框中选择一个项目,但当我选择它时,它会生成一个秘密异常。我的代码如下: `


无效打印文本(对象发送者,选择ChangedEventArgs args)
{
ListBoxItem lbi=((发件人作为ListBox)。选择EdItem作为ListBoxItem);
字符串a=lbi.Content.ToString();
Window1 neww=null;
neww=新窗口1();
neww.Show();
}

`我不知道我在哪里做错了,请引导我。我附上它的图像,让你更清楚。谢谢大家!

如果这是你的答案,那么请选择它作为答案,这样其他人就不必再次阅读你的整个问题
void PrintText(object sender, SelectionChangedEventArgs args)
{
  object item = listBox1.SelectedItem;

  if (item == null) {
    txtSelectedItem.Text = "No item currently selected.";
  } else {
    txtSelectedItem.Text = item.ToString();
  }

  // ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
  //  String a =  lbi.Content.ToString();
  Window1 neww = null;
  neww = new Window1();
  neww.Show();
}
void PrintText(object sender, SelectionChangedEventArgs args)
{
  object item = listBox1.SelectedItem;

  if (item == null) {
    txtSelectedItem.Text = "No item currently selected.";
  } else {
    txtSelectedItem.Text = item.ToString();
  }

  // ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
  //  String a =  lbi.Content.ToString();
  Window1 neww = null;
  neww = new Window1();
  neww.Show();
}