【发布时间】:2013-06-25 22:05:35
【问题描述】:
我在 wpf 应用程序中有一个 listBox,它包含两个条目。我已经为它编写了双击事件函数。但是当我单击任何单个条目时,它会显示NullReferenceException。例外是在线 - if (listBox1.SelectedItem != null)
我只想要一个我会点击的条目。我应该如何进行?
我的双击事件如下:
private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//Submit clicked Entry
if (listBox1.SelectedItem != null)
{
Harvest_TimeSheetEntry entryToPost = (Harvest_TimeSheetEntry)listBox1.SelectedItem;
if (!entryToPost.isSynced)
{
//Check if something is selected in selectedProjectItem For that item
if (entryToPost.ProjectNameBinding == "Select Project")
MessageBox.Show("Please Select a Project for the Entry");
else
Globals._globalController.harvestManager.postHarvestEntry(entryToPost);
}
else
{
//Already synced.. Make a noise or something
MessageBox.Show("Already Synced;TODO Play a Sound Instead");
}
}
else
{
throw new NullReferenceException("Entry does not exist");
}
}
我将事件处理程序分配为,
InitializeComponent();
listBox1.MouseDoubleClick += new MouseButtonEventHandler(listBox1_MouseDoubleClick);
【问题讨论】:
-
你能显示 listBox1 标记吗?
-
这是 WPF?对我来说看起来像 WinForms。为什么不通过利用数据绑定和对绑定集合进行操作来获取 WPF 的功能呢?
-
是的。我试过了。它在我提到的第一行抛出异常。
-
@Marius 没有什么可以阻止程序员使用 WPF 中的代码,但不鼓励使用 MVVM。
-
listBox1 标记-
标签: c# wpf nullreferenceexception double-click listboxitem