WPF 数据虚拟化和数据网格

5

我正在使用Paul McClean所描述的数据虚拟化:http://www.codeproject.com/KB/WPF/WpfDataVirtualization.aspx

它在ListView控件中运行良好。

但是,当我将其与DataGrid控件(AsyncVirtualizationCollection)一起使用时,会抛出异常:

"值不能为null,参数名称:key"

我不知道原因以及如何防止发生。我需要DataGrid控件的编辑功能。


嗨, Attilah,你能指导我如何在DataGrid中转换或使用上面的示例吗? 我完全是WPF的新手。 - Arijit Mukherjee
3个回答

6
我也遇到了这个问题。事实证明,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设置为一个虚拟对象,该对象在加载过程中显示。

3

您是否正在使用字典?请调试并检查是否尝试将空值作为字典中的键添加。或者检查在网格视图上是否有一个DataKeyNames参数,其中包含一个您尝试插入的空键。

只需在加载/填充数据的位置(F10 / F11)调试即可。在Visual Studio中观察您的本地窗口。


0
我发现这个异常实际上是在 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) 未知

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接