获得;密钥不能为空";在C#对象上实现INotifyPropertyChanged后

获得;密钥不能为空";在C#对象上实现INotifyPropertyChanged后,c#,wpf,observablelist,C#,Wpf,Observablelist,我正在Visual Studio 2017中开发C#4.5.2中的WPF应用程序。在应用程序中,我有一个自定义对象,它被卷入一个observateCollection,供以后处理。如有必要,我希望能够处理此CollectionChanged事件 我向类添加了适当的修饰(:INotifyPropertyChanged),添加了事件和处理程序,并使用接口PropertyChanged对对象中的每个属性进行了属性化: public class OrderLineItem : INotifyProper

我正在Visual Studio 2017中开发C#4.5.2中的WPF应用程序。在应用程序中,我有一个自定义对象,它被卷入一个
observateCollection
,供以后处理。如有必要,我希望能够处理此
CollectionChanged
事件

我向类添加了适当的修饰(
:INotifyPropertyChanged
),添加了事件和处理程序,并使用接口
PropertyChanged
对对象中的每个属性进行了属性化:

public class OrderLineItem : INotifyPropertyChanged
{
    private int _lineItemNumber;
    public int LineItemNumber
    {
        get => _lineItemNumber;
        set { _lineItemNumber = value; OnPropertyChanged(); }
    }

    private int _quantity;
    public int Quantity
    {
        get => _quantity;
        set { _quantity = value; OnPropertyChanged(); }

    }

    private string _partNumber;
    public string PartNumber
    {
        get => _partNumber;
        set { _partNumber = value; OnPropertyChanged(); }

    }

    private Hinge _hinged;
    public Hinge Hinging
    {
        get => _hinged;
        set { _hinged = value; OnPropertyChanged(); }
    }

    private Finish _finished;
    public Finish Finished
    {
        get => _finished;
        set { _finished = value; OnPropertyChanged(); }
    }

    private decimal _unitPrice;
    public decimal UnitPrice
    {
        get => _unitPrice;
        set { _unitPrice = value; OnPropertyChanged(); }
    }

    private decimal _modifyPrice;
    public decimal ModifyPrice
    {
        get => _modifyPrice;
        set { _modifyPrice = value; OnPropertyChanged(); }
    }

    private decimal _extendedPrice;
    public decimal ExtendedPrice
    {
        get => _extendedPrice;
        set { _extendedPrice = value; OnPropertyChanged(); }
    }

    private List<string> _modifications;
    public List<string> Modifications
    {
        get => _modifications;
        set { _modifications = value; OnPropertyChanged(); }
    }

    private CabinetType _type;
    public CabinetType Type
    {
        get => _type;
        set { _type = value; OnPropertyChanged(); }
    }

    private string _display;
    public string Display
    {
        get => _display;
        set { _display = value; OnPropertyChanged(); }
    }

    public enum Hinge { None = 0, L, R, BD }
    public enum Finish { None = 0, L, R, B }

    public OrderLineItem()
    {
        LineItemNumber = -1;
        Quantity = -1;
        PartNumber = string.Empty;
        Hinging = Hinge.None;
        Finished = Finish.None;
        UnitPrice = 0.00m;
        ModifyPrice = 0.00m;
        ExtendedPrice = 0.00m;
        Modifications = new List<string>();
        Type = CabinetType.None;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
每当我向集合中添加项目时,我都会向其添加
PropertyChanged
处理程序:

...

item.PropertyChanged += ItemOnPropertyChanged;

...
此集合通过
.ItemsSource
属性显示在数据网格中

此问题标题中的问题仅在实现
INotifyPropertyChanged
及其关联的方法/接口等后发生

我能够获得的堆栈跟踪没有为我提供任何有用的信息(行号、文件、方法等)来尝试调试它。我确实启用了所有调试选项,以确保没有忽略任何潜在的异常。显示的行号如下所示:

 AppDomain.CurrentDomain.UnhandledException += 
  (sender, args) => throw new Exception("Unhandled exception: " + args.ExceptionObject);
如果我没有这一行,或者把它注释掉


除了不实现
CollectionChanged
事件之外,你知道我应该如何解决这个问题吗?

看看你的异常,这个问题似乎与从HybridDictionary中搜索空键有关

我猜INotifyPropertyChanged实现只是触发了这个HybridDictionary[null]查找,与根本问题无关


在更高的层次上,这似乎是VS调试器应该直接跳到的地方,因此这似乎很奇怪,我建议您深入了解您的环境,以确保调试工作正常。

查看您的异常,似乎问题与从HybridDictionary中搜索空键有关

我猜INotifyPropertyChanged实现只是触发了这个HybridDictionary[null]查找,与根本问题无关


在更高的层次上,这似乎是VS调试器应该直接引导您去做的事情,因此这似乎很奇怪,我建议您深入了解您的环境,以确保调试正常工作。

只是一件小事-您的代码具有
newItem.PropertyChanged+=onimpropertychanged
,但事件处理程序的名称中通常没有
的部分-约定是以
开头的方法在那里引发事件,而不是处理它们。@Enigmativity很好,我相信ReSharper可能已经自动命名了该方法。你能为你的DataGrid发布XAML源代码吗?我在类似的情况下遇到了相同的异常,通过显式设置绑定的Path属性(例如,将Binding=“{Binding(a:B.C)}”更改为Binding=“{Binding Path=(a:B.C)}”)解决了这个问题。我不知道为什么会这样。只是一件小事-你的代码有
newItem.PropertyChanged+=OnItemPropertyChanged
,但事件处理程序的名称中通常没有
的部分-约定是以
开头的方法在那里引发事件,而不是处理它们。@Enigmativity很好,我相信ReSharper可能已经自动命名了该方法。你能为你的DataGrid发布XAML源代码吗?我在类似的情况下遇到了相同的异常,通过显式设置绑定的Path属性(例如,将Binding=“{Binding(a:B.C)}”更改为Binding=“{Binding Path=(a:B.C)}”)解决了这个问题。我不知道为什么会这样。
 AppDomain.CurrentDomain.UnhandledException += 
  (sender, args) => throw new Exception("Unhandled exception: " + args.ExceptionObject);