【问题标题】:DataGrid throws NullReferenceExceptionDataGrid 抛出 NullReferenceException
【发布时间】:2014-03-04 09:02:39
【问题描述】:

我有一个应用程序在 TabControl 的 tabitems 中显示一些数据网格。在其中部署了它的一台机器上——而且只有一台——切换选项卡会在没有任何警告的情况下使应用程序崩溃。 我无法在那台机器上调试应用程序。我对崩溃的唯一指示来自事件查看器。这是异常堆栈跟踪:

框架版本:v4.0.30319 描述:进程被终止 由于未处理的异常。异常信息: System.NullReferenceException 堆栈:在 System.Windows.Controls.Primitives.DataGridColumnHeader.SetLeftGripperVisibility() 在 System.Windows.Controls.Primitives.DataGridColumnHeader.HookupGripperEvents() 在 System.Windows.FrameworkElement.ApplyTemplate() 在 System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) 在 System.Windows.UIElement.Measure(System.Windows.Size) 在 System.Windows.Controls.DataGridCellsPanel.MeasureChild(System.Windows.UIElement, System.Windows.Size)在 System.Windows.Controls.DataGridCellsPanel.GenerateChild(System.Windows.Controls.Primitives.IItemContainerGenerator, System.Windows.Size、System.Windows.Controls.DataGridColumn、Int32 ByRef, System.Windows.Size ByRef) 在 System.Windows.Controls.DataGridCellsPanel.GenerateChild(System.Windows.Controls.Primitives.IItemContainerGenerator, System.Windows.Size,System.Windows.Controls.DataGridColumn, System.IDisposable ByRef、Int32 ByRef、System.Windows.Size ByRef)
在 System.Windows.Controls.DataGridCellsPanel.DetermineRealizedColumnsBlockList(System.Windows.Size) 在 System.Windows.Controls.DataGridCellsPanel.MeasureOverride(System.Windows.Size) 在 System.Windows.FrameworkElement.MeasureCore(System.Windows.Size)
在 System.Windows.UIElement.Measure(System.Windows.Size) 在 System.Windows.ContextLayoutManager.UpdateLayout() 在 System.Windows.ContextLayoutManager.UpdateLayoutCallback(System.Object) 在 System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
在 System.Windows.Media.MediaContext.RenderMessageHandlerCore(System.Object) 在 System.Windows.Media.MediaContext.RenderMessageHandler(System.Object) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) 在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate、System.Object、Int32、System.Delegate)在 System.Windows.Threading.Dispatcher.WrappedInvoke(System.Delegate, System.Object,Int32,System.Delegate)在 System.Windows.Threading.DispatcherOperation.InvokeImpl() 在 System.Threading.ExecutionContext.runTryCode(System.Object) 在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode,System.Object)在 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback,System.Object,布尔值)在 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback,System.Object)在 System.Windows.Threading.DispatcherOperation.Invoke() 在 System.Windows.Threading.Dispatcher.ProcessQueue() 在 System.Windows.Threading.Dispatcher.WndProcHook(IntPtr,Int32,IntPtr, MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) 在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) 在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate、System.Object、Int32、System.Delegate)在 System.Windows.Threading.Dispatcher.WrappedInvoke(System.Delegate, System.Object,Int32,System.Delegate)在 System.Windows.Threading.Dispatcher.InvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32) 在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
在 MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef) 在 System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame) 在 System.Windows.Application.RunInternal(System.Windows.Window) 在 System.Windows.Application.Run() 在 MyApp.App.Main()

这一切都发生在外部代码中。 我尝试从 DataGrid 继承,覆盖 ApplyTemplate 方法并在其中放置一个 try catch。但似乎这是不允许的,由此产生的控制是不可用的。 对于如何解决这个问题,有任何的建议吗? 谢谢

【问题讨论】:

    标签: wpf exception datagrid tabcontrol


    【解决方案1】:

    它看起来像一个错误,您应该联系微软支持团队。不过你可以试试另一种选择。

    Here你可以找到测试版的DataGridColumnHeader源代码。

    编辑

    根据该源代码,我的猜测是,如果您关闭列调整大小或与列标题的“Grip”区域相关的任何内容,它可能不再抛出异常。

    【讨论】:

    • 感谢您的意见。从 DataGridColumnHeader 派生后,如何让 DataGrid 使用派生版本?
    • 你说得对,我没有先检查。我的意思是创建一组带有自定义标题的自定义DataGridColumns。但不幸的是,它无法完成。很抱歉误导了你。我更新了我的答案,看看它是否有帮助。
    • 不用担心。我会尝试禁用调整大小,但我担心这太严格了......
    • 如果这不起作用或造成更多麻烦,请尝试在该计算机上重新安装 .net 框架或其他先决条件。甚至升级到最新的操作系统可能会有所帮助。
    • 禁用列调整大小现在可以工作,尽管它不是一个理想的解决方案。我会用 .net 4.5 做一些测试,希望能得到修复。
    猜你喜欢
    • 2013-05-30
    • 2020-11-17
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多