在我的 WPF 应用程序中,我为列表框项编写了一个双击事件。当我双击列表框的现有条目时,该条目成功提交。
这是代码:
private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//Submit clicked Entry
try
{
ListBoxItem item = (ListBoxItem)sender;
Harvest_TimeSheetEntry entryToPost = (Harvest_TimeSheetEntry)item.Content;
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");
}
}
catch (Exception)
{ }
}
但是当我从项目组合框中选择项目名称时,它会向我显示一个异常——TargetInvocationException。内部异常是: System.InvalidCastException , System.FormatException。
我认为,问题出在 projectNamebinding 的设置器内部。那么我应该如何进行呢?
projectNameBinding 的代码是:
public class HarvestTimeSheetEntry
{
private int _projectid { get; set; }
public string ProjectNameBinding
{
set
{
this._projectid = Int32.Parse(value);
}
get
{
if (entrySource == source.harvest)
return Globals._globalController.harvestManager.getProjectEntriesThroughId(this._projectid)._name;
else if (entrySource == source.googlecalendar)
return "Select Project";
else if (entrySource == source.processInfo)
return "Select Project";
else
return "Whaaa?";
}
}