5

我将 C# 与 .Net 4.0 和 WPF DataGrid 一起使用。有时我在程序执行过程中会出现以下异常。如果我更改某些列的值,将引发异常。DataGrid 的虚拟化已激活(但仅对于行,列虚拟化设置为 false)。现在我需要知道它是否是.Net 内部的错误。

An unhandled exception occured in GUI. Stack Trace - "System.NullReferenceException:     
Object reference not set to an instance of an object.
at System.Windows.Controls.DataGridCell.OnCoerceIsReadOnly(DependencyObject d, Object    
baseValue)
at System.Windows.DependencyObject.ProcessCoerceValue(DependencyProperty dp,   
PropertyMetadata metadata, EntryIndex& entryIndex, Int32& targetIndex,   
EffectiveValueEntry& newEntry, EffectiveValueEntry& oldEntry, Object& oldValue, Object    
baseValue, Object controlValue, CoerceValueCallback coerceValueCallback, Boolean   
coerceWithDeferredReference, Boolean coerceWithCurrentValue, Boolean 
skipBaseValueChecks)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex,    
DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry,   
EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean 
coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.CoerceValue(DependencyProperty dp)
at System.Windows.Controls.DataGridHelper.TransferProperty(DependencyObject d,  
DependencyProperty p)
at System.Windows.Controls.DataGridCell.PrepareCell(Object item, DataGridRow ownerRow,    
Int32 index)
at System.Windows.Controls.Primitives.DataGridCellsPresenter.PrepareContainerForItemOverride( 
DependencyObject element, Object item)
at System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(DependencyObject container, Object item)
at  
System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(DependencyObject container)
at System.Windows.Controls.DataGridCellsPanel.InsertContainer(Int32 childIndex, UIElement container, Boolean isRecycled)
at System.Windows.Controls.DataGridCellsPanel.AddContainerFromGenerator(Int32 childIndex, UIElement child, Boolean newlyRealized)
at System.Windows.Controls.DataGridCellsPanel.GenerateChild(IItemContainerGenerator generator, Size constraint, DataGridColumn column, Int32& childIndex, Size& childSize)
at System.Windows.Controls.DataGridCellsPanel.GenerateChildren(IItemContainerGenerator generator, Int32 startIndex, Int32 endIndex, Size constraint)
at System.Windows.Controls.DataGridCellsPanel.GenerateAndMeasureChildrenForRealizedColumns(Size constraint)
at System.Windows.Controls.DataGridCellsPanel.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
at System.Windows.Controls.ItemsPresenter.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Control.MeasureOverride(Size constraint)
at System.Windows.Controls.Primitives.DataGridCellsPresenter.MeasureOverride(Size availableSize)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Border.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Control.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
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.ContextLayoutManager.UpdateLayoutCallback(Object arg)
at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
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)"
4

1 回答 1

2

我有同样的问题,我尝试了下面的代码,它得到了修复。

  /// <summary>
  /// -MyExtndDataGrid an extension of datagrid
  /// </summary>
  public class MyExtndDataGrid : DataGrid
  {
    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    protected override AutomationPeer OnCreateAutomationPeer()
    {
      return null;
    }
  }

and in the XAML

 <local:MyExtndDataGrid ...../>
于 2012-11-22T06:17:44.290 回答