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