wpf数据网格错误/错误

时间:2010-05-26 07:29:31

标签: wpf datagrid

我遇到了WPF DataGrid的问题。

我正在使用WPF DataGrid& DataGridTemplateColumn(文本框)

有时我会收到以下错误。插入数据时,我会出现错误。没有模式可以重现此错误。它是随机发生的。

  

System.NullReferenceException:Object   引用未设置为的实例   对象

详细信息:

System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
  Source="PresentationFramework"
  StackTrace:
       at System.Windows.Controls.VirtualizingStackPanel.InsertContainer(Int32 childIndex, UIElement container, Boolean isRecycled)
       at System.Windows.Controls.VirtualizingStackPanel.AddContainerFromGenerator(Int32 childIndex, UIElement child, Boolean newlyRealized)
       at System.Windows.Controls.VirtualizingStackPanel.BringIndexIntoView(Int32 index)
       at Microsoft.Windows.Controls.Primitives.DataGridRowsPresenter.InternalBringIndexIntoView(Int32 index) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\Primitives\DataGridRowsPresenter.cs:line 48
       at Microsoft.Windows.Controls.DataGrid.ScrollRowIntoView(Object item) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:line 1617
       at Microsoft.Windows.Controls.DataGrid.ScrollCellIntoView(Object item, DataGridColumn column) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:line 1638
       at Microsoft.Windows.Controls.DataGrid.ScrollIntoView(Object item, DataGridColumn column) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:line 1544
       at Microsoft.Windows.Controls.DataGrid.OnEnterKeyDown(KeyEventArgs e) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:line 5473
       at Microsoft.Windows.Controls.DataGrid.OnKeyDown(KeyEventArgs e) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:line 5101
       at System.Windows.UIElement.OnKeyDownThunk(Object sender, KeyEventArgs e)
       at System.Windows.Input.KeyEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       at System.Windows.Input.InputManager.ProcessStagingArea()
       at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
       at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
       at System.Windows.Interop.HwndKeyboardInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawKeyboardActions actions, Int32 scanCode, Boolean isExtendedKey, Boolean isSystemKey, Int32 virtualKey)
       at System.Windows.Interop.HwndKeyboardInputProvider.ProcessKeyAction(MSG& msg, Boolean& handled)
       at System.Windows.Interop.HwndSource.CriticalTranslateAccelerator(MSG& msg, ModifierKeys modifiers)
       at System.Windows.Interop.HwndSource.OnPreprocessMessage(Object param)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
  InnerException:

但是当虚拟化设置为False时,它就可以正常工作。

有什么我想念的吗?需要一个解决方案。

提前致谢。

1 个答案:

答案 0 :(得分:3)

您是手动还是通过代码插入数据?

如果用手,那么:

1)当编辑的texbox失去焦点时,异常会弹出什么时候?

2)似乎你正在使用datagrid的“工具包”版本。它是最新的(2月10日,我认为是最新的)?你在.NET 4中尝试过内置的数据网格吗?

如果在代码中:

如果您尝试为编辑的项目调用ScrollIntoView,则该调用必须以datagrid.UpdateLayout()开头。然后,如果您尝试为项目生成DataGridRow容器,那么在获取DataGridRow之后,您还必须在设置任何属性或尝试调用其上的方法之前调用它上面的UpdateLayout()。 这完全归功于虚拟化工作的不寻常方式(至少在数据网格中)。如果你没有像我提到的那样调用UpdateLayout,你会在某些情况下获得(null ref。)异常,但在其他情况下则不会。