Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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# mscorlib.dll中发生类型为“System.Reflection.TargetInvocationException”的第一次意外异常_C#_Wpf_Set_Listboxitem_.net - Fatal编程技术网

C# mscorlib.dll中发生类型为“System.Reflection.TargetInvocationException”的第一次意外异常

C# mscorlib.dll中发生类型为“System.Reflection.TargetInvocationException”的第一次意外异常,c#,wpf,set,listboxitem,.net,C#,Wpf,Set,Listboxitem,.net,在我的WPF应用程序中,我为listbox项编写了一个双击事件。当我双击listbox的现有条目时,该条目成功提交 这是代码: private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e) { //Submit clicked Entry try { ListBoxItem item = (ListBoxItem)sende

在我的WPF应用程序中,我为listbox项编写了一个双击事件。当我双击listbox的现有条目时,该条目成功提交

这是代码:

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)
        { }
     }
但是,当我从项目组合框中选择projectname时,它会向我显示一个异常-targetingException。 内部异常为:System.InvalidCastException、System.FormatException

我认为,问题在于projectNamebinding的setter内部。那么,我应该如何继续

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?";
        }
}

在setter中放置一个断点,如果它被击中,请逐步执行,直到您找到罪犯,并且还有用于记录的计时器。因此,在启动计时器20秒后,listbox中会生成一个新条目。然后,当我从组合框中选择project时,它会在该条目中显示一个异常。为什么要将.Convert.ToInt32this.\u projectid?它已经是int。@Clemens,这是个错误。我更正了它。但从快速浏览中仍然显示相同的异常,您的问题可能是您似乎试图将ListBox强制转换为双击处理程序中的ListBoxItem。