gpt4 book ai didi

c# - 在 C# 对象上实现 INotifyPropertyChanged 后获取 "Key cannot be null"

转载 作者:行者123 更新时间:2023-12-05 06:38:54 25 4
gpt4 key购买 nike

我正在 Visual Studio 2017 中使用 C# 4.5.2 开发一个 WPF 应用程序。在该应用程序中,我有一个自定义对象,该对象被滚动到 ObservableCollection<T> 中。供以后处理。我希望能够处理这个 CollectionChanged必要时/必要时的事件。

我向类 ( : INotifyPropertyChanged ) 添加了适当的装饰,添加了事件和处理程序,并使用接口(interface) 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));
}
}

在我的 MainWindow.xaml.cs文件我添加了 .CollectionChanged我的 ObservableCollection<T> 的处理程序和 .PropertyChanged我的对象的处理程序:

private void AddedItemsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (OrderLineItem newItem in e.NewItems)
{
_addedItems.Add(newItem);
newItem.PropertyChanged += OnItemPropertyChanged;
}
}

if (e.OldItems != null)
{
foreach (OrderLineItem oldItem in e.OldItems)
{
_addedItems.Add(oldItem);
oldItem.PropertyChanged -= OnItemPropertyChanged;
}
}
}

private void OnItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
OrderLineItem item = sender as OrderLineItem;
if (item != null) _addedItems.Add(item);
}

每当我向集合中添加项目时,我都会添加 PropertyChanged它的处理程序:

...

item.PropertyChanged += ItemOnPropertyChanged;

...

此集合通过 .ItemsSource 显示在 DataGrid 中属性(property)。

此问题标题中的问题发生在实现INotifyPropertyChanged 之后及其相关的方法/接口(interface)等。

我能够获得的堆栈跟踪没有为我提供任何有用的信息(行号、文件、方法等)来尝试调试它。我确实启用了所有调试选项以确保我没有忽略任何潜在的异常。 Here is a pastebin of the exception .指示的行号是这样的:

 AppDomain.CurrentDomain.UnhandledException += 
(sender, args) => throw new Exception("Unhandled exception: " + args.ExceptionObject);

如果我没有这一行,或者把它注释掉this is a pastebin of the exception I get instead .

除了实现 CollectionChanged 之外,关于我应该如何继续尝试解决这个问题的任何想法事件?

最佳答案

似乎与 INotifyPropertyChanged 和绑定(bind)规则有关,但我不知道是哪一个。

我遇到了同样的问题:在 PropertyGrid 项上实现 INotifyPropertyChanged 触发了 Key cannot be null 绑定(bind)错误。顺便说一下,这是我见过的最无用和随机的错误日志之一。

Cannot save value from target back to source.
BindingExpression:[info on an unrelated parent binding...]
ArgumentNullException:'System.ArgumentNullException: Key cannot be null.

一段时间后,我发现旧 wpf 版本中的奇怪绑定(bind)行为,在绑定(bind)中添加 Path= 可以修复它,但事实并非如此。

我实际上在代码隐藏中有一个像这样的绑定(bind):

void GetBinding(PropertyItem propertyItem)
{
return new Binding($"({propertyItem.PropertyName})")
{
//[Source, Mode, etc...]
}
}

并且代码对属性和附加属性都有效,但在此类上实现属性通知程序后立即停止工作。也许更严格的规则被触发并将我的属性(property)标记为不是“附加”属性(property),但它在没有界面的情况下工作。

所以,我删除了括号,它现在可以工作了。

  return new Binding($"{propertyItem.PropertyName}")

关于c# - 在 C# 对象上实现 INotifyPropertyChanged 后获取 "Key cannot be null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45382079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com