我正在使用Paul McClean所描述的数据虚拟化:http://www.codeproject.com/KB/WPF/WpfDataVirtualization.aspx
它在ListView控件中运行良好。
但是,当我将其与DataGrid控件(AsyncVirtualizationCollection)一起使用时,会抛出异常:
"值不能为null,参数名称:key"
我不知道原因以及如何防止发生。我需要DataGrid控件的编辑功能。
我正在使用Paul McClean所描述的数据虚拟化:http://www.codeproject.com/KB/WPF/WpfDataVirtualization.aspx
它在ListView控件中运行良好。
但是,当我将其与DataGrid控件(AsyncVirtualizationCollection)一起使用时,会抛出异常:
"值不能为null,参数名称:key"
我不知道原因以及如何防止发生。我需要DataGrid控件的编辑功能。
AsyncVirtualizingCollection
的基类VirtualizingCollection
中有一个问题代码:public T this[int index]
{
// snip
// defensive check in case of async load
if (_pages[pageIndex] == null)
return default(T);
// snip
}
T
是一个引用类型,那么default(T)
就是null
,而DataGrid
不接受空的行对象。VirtualizingCollection
中添加了一个公共属性来保存默认值:
public T DefaultValue = default(T);
并将以上代码更改为返回DefaultValue
而不是default(T)
。然后,在构造我的AsyncVirtualizingCollection
时,我将DefaultValue
设置为一个虚拟对象,该对象在加载过程中显示。您是否正在使用字典?请调试并检查是否尝试将空值作为字典中的键添加。或者检查在网格视图上是否有一个DataKeyNames参数,其中包含一个您尝试插入的空键。
只需在加载/填充数据的位置(F10 / F11)调试即可。在Visual Studio中观察您的本地窗口。
DataGridItemAttachedStorage
类中发生的。下面是一些调用堆栈帧,希望有人能够提供一些线索。对于我的糟糕英语表示抱歉。mscorlib.dll!System.Collections.Generic.Dictionary<object,System.Collections.Generic.Dictionary<System.Windows.DependencyProperty,object>>.FindEntry(object key) 未知
mscorlib.dll!System.Collections.Generic.Dictionary<object,System.Collections.Generic.Dictionary<System.Windows.DependencyProperty,object>>.TryGetValue(object key, out System.Collections.Generic.Dictionary<System.Windows.DependencyProperty,object> value) 未知
PresentationFramework.dll!System.Windows.Controls.DataGridItemAttachedStorage.TryGetValue(object item, System.Windows.DependencyProperty property, out object value) 未知
PresentationFramework.dll!System.Windows.Controls.DataGridRow.RestoreAttachedItemValue(System.Windows.DependencyObject objectWithProperty, System.Windows.DependencyProperty property) 未知
PresentationFramework.dll!System.Windows.Controls.DataGridRow.SyncProperties(bool forcePrepareCells) 未知
PresentationFramework.dll!System.Windows.Controls.DataGridRow.PrepareRow(object item, System.Windows.Controls.DataGrid owningDataGrid) 未知
PresentationFramework.dll!System.Windows.Controls.DataGrid.PrepareContainerForItemOverride(System.Windows.DependencyObject element, object item) 未知
PresentationFramework.dll!System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(System.Windows.DependencyObject container, object item) 未知