我有一个奇怪的情况,我已经实现了一个ObservableConcurrentDictionary(基于 .NET 的ConcurrentDictionary),我用它来实现ObservableConcurrentCollection(它只是自动在 Dictionary 中设置 Key 属性,并且具有所有 IList、IEnumerable , IQueryable 和 ICollection 方法)。当我执行以下操作时,这一切都很好:
ObservableConcurrentCollection<string> items = new ObservableConcurrentCollection<string>();
dataGrid.ItemsSource = items;
items.Add("TEST");
这在 DataGrid 中得到了完美体现。
但是,当我这样做时:
ObservableConcurrentCollection<string> items = new ObservableConcurrentCollection<string>();
items.Add("TEST"); // <-- Notice that this and the line below are swapped.
dataGrid.ItemsSource = items;
它无法正常工作,因为“项目”中的项目突然变成了 KeyValuePair。我可以通过在最后一行中使用 dataGrid.ItemsSource = items.Values 轻松解决此问题,但我宁愿让它像前一个一样工作(而且它也令人困惑)。