WPF DataGrid EnableColumnVirtualization ="True"在 DataGridCellsPanel.get_HasCorrectRealizedColumns() 处引发 NullReferenceException

标签 wpf wpfdatagrid wpf-4.0

我有一个带有 EnableColumnVirtualization="True" 的 WPF DataGrid .用户控件会显示,但只要您滚动或调整大小,您就会收到 NullReferenceExceptionDataGridCellsPanel.get_HasCorrectRealizedColumns() .设置 ColumnVirtualization="False"纠正问题。我试过删除 EnableRowVirtualization="True" ...等认为这可能是由于属性的组合,但我能够用一个非常简单的 DataGrid 重现这个问题:

<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="True" EnableColumnVirtualization="True"

有没有其他人遇到过这个问题?我正在运行 Win7 64。谢谢大家!
   at System.Windows.Controls.DataGridCellsPanel.get_HasCorrectRealizedColumns()
   at System.Windows.Controls.Primitives.DataGridCellsPresenter.SyncProperties(Boolean forcePrepareCells)
   at System.Windows.Controls.DataGridRow.SyncProperties(Boolean forcePrepareCells)
   at System.Windows.Controls.DataGridRow.PrepareRow(Object item, DataGrid owningDataGrid)
   at System.Windows.Controls.DataGrid.PrepareContainerForItemOverride(DependencyObject element, Object item)
   at System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(DependencyObject container, Object item)
   at System.Windows.Controls.VirtualizingStackPanel.InsertContainer(Int32 childIndex, UIElement container, Boolean isRecycled)
   at System.Windows.Controls.VirtualizingStackPanel.AddContainerFromGenerator(Int32 childIndex, UIElement child, Boolean newlyRealized, Boolean isBeforeViewport)
   at System.Windows.Controls.VirtualizingStackPanel.MeasureChild(IItemContainerGenerator& generator, IContainItemStorage& itemStorageProvider, IContainItemStorage& parentItemStorageProvider, Object& parentItem, Boolean& hasUniformOrAverageContainerSizeBeenSet, Double& computedUniformOrAverageContainerSize, Boolean& computedAreContainersUniformlySized, IList& items, Object& item, IList& children, Int32& childIndex, Boolean& visualOrderChanged, Boolean& isHorizontal, Size& childConstraint, Rect& viewport, VirtualizationCacheLength& cacheSize, VirtualizationCacheLengthUnit& cacheUnit, Boolean& foundFirstItemInViewport, Double& firstItemInViewportOffset, Size& stackPixelSize, Size& stackPixelSizeInViewport, Size& stackPixelSizeInCacheBeforeViewport, Size& stackPixelSizeInCacheAfterViewport, Size& stackLogicalSize, Size& stackLogicalSizeInViewport, Size& stackLogicalSizeInCacheBeforeViewport, Size& stackLogicalSizeInCacheAfterViewport, Boolean& mustDisableVirtualization, Boolean isBeforeFirstItem, Boolean isAfterFirstItem, Boolean isAfterLastItem, Boolean skipActualMeasure, Boolean skipGeneration, Boolean& hasBringIntoViewContainerBeenMeasured, Boolean& hasVirtualizingChildren)
   at System.Windows.Controls.VirtualizingStackPanel.MeasureOverrideImpl(Size constraint, Nullable`1& lastPageSafeOffset, List`1& previouslyMeasuredOffsets, Boolean remeasure)
   at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)
   at System.Windows.Controls.Primitives.DataGridRowsPresenter.MeasureOverride(Size constraint)
   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at System.Windows.ContextLayoutManager.UpdateLayout()
   at System.Windows.Controls.VirtualizingStackPanel.<>c__DisplayClassa.<InitializeViewport>b__7()
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run()
   at ....App.Main() in c:\....\obj\Debug\App.g.cs:line 0

最佳答案

我可以通过更改 VirtualizationMode 来实现它从回收到标准。

    <DataGrid ItemsSource="{Binding Items}" 
     AutoGenerateColumns="True"
     EnableColumnVirtualization="True"
     Standard.VirtualizingStackPanel.IsVirtualizing="True"
     VirtualizingStackPanel.VirtualizationMode="Standard">

我仍然想知道为什么回收不起作用......错误?

希望这可以帮助遇到同样问题的任何人。

关于WPF DataGrid EnableColumnVirtualization ="True"在 DataGridCellsPanel.get_HasCorrectRealizedColumns() 处引发 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20098908/

相关文章:

c# - 如何通过设置窗口实时控制程序的不透明度?

wpf - Silverlight 绑定(bind)到字典中的项目

c# - WPF Sentinel 对象以及如何检查内部类型

c# - 当弹出窗口打开时,单击 Windows 标题栏的最小化、最大化和关闭按钮不起作用

c# - 从另一个 View 模型调用函数时 RaisePropertyChanged 不会触发

wpf - 如何同步关闭 WPF 应用程序?

c# - 没有为控件模板中的数据网格设置依赖属性

wpf - 如何将 DataGrid 中 CheckBox 的可见性绑定(bind)到该行的 DataContext?

c# - 为什么DataGrid调用集合的IndexOf传递类型ItemcControl.ItemInfo的对象?

c# - WPF 4 中的 VistaBridge 和桌面窗口管理器