wpf数据网格错误/错误 [英] wpf data grid bug/error

查看:241
本文介绍了wpf数据网格错误/错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面对WPF DataGrid的问题。



我使用WPF DataGrid& DataGridTemplateColumn(textboxes)



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


System.NullReferenceException:Object
引用未设置为
对象的实体。 / p>

详细信息:

 
System.NullReferenceException被用户代码未处理
Message =对象引用未设置为对象的实例。
Source =PresentationFramework
StackTrace:
在System.Windows.Controls.VirtualizingStackPanel.InsertContainer(Int32 childIndex,UIElement container,Boolean isRecycled)
在System.Windows.Controls。 VirtualizationStackPanel.AddContainerFromGenerator(Int32 childIndex,UIElement child,Boolean newRealized)
在System.Windows.Controls.VirtualizingStackPanel.BringIndexIntoView(Int32索引)
在Microsoft.Windows.Controls.Primitives.DataGridRowsPresenter.InternalBringIndexIntoView(Int32索引)在C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\Primitives\DataGridRowsPresenter.cs:line 48
在Microsoft.Windows.Controls.DataGrid.ScrollRowIntoView(对象项)在C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\\ \\Controls\DataGrid.cs:行1617
在Microsoft.Windows.Controls.DataGrid.ScrollCellIntoView(对象项,DataGridColumn列)C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows \Controls\DataGrid.cs:行1638
在Microsoft.Windows.Controls.DataGrid.ScrollIntoView(对象项,DataGridColumn列)在C:\dd\WPF_1\src\wpf\src \ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:行1544
在Microsoft.Windows.Controls.DataGrid.OnEnterKeyDown(KeyEventArgs e)在C:\dd \WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:line 5473
在Microsoft.Windows.Controls.DataGrid .OnKeyDown(KeyEventArgs e)在C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\\ \\Controls\DataGrid.cs:line 5101
在System.Windows.UIElement.OnKeyDownThunk(Object sender,KeyEventArgs e)
在System.Windows.Input.KeyEventArgs.InvokeEventHandler(Delegate genericHandler,Object genericTarget)
在System.Windows.RoutedEventArgs.InvokeHandler(委托处理程序,对象目标)
在System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target,RoutedEventArgs routedEventArgs)
在System.Windows.EventRoute.InvokeHandlersImpl (Object源,RoutedEventArgs args,Boolean reRaised)
在System.Windows.UIElement.RaiseEventImpl(DependencyObject sender,RoutedEventArgs args)
在System.Windows.UIElement.RaiseEvent(RoutedEventArgs args,Boolean trusted)
在System.Windows.Input.InputManager.ProcessStagingArea()
在System.Windows.Input.InputManager.ProcessInput(InputEventArgs输入)
在System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
在System.Windows.Interop.HwndKeyboardInputProvider.ReportInput(IntPtr hwnd,InputMode模式,Int32时间戳,RawKeyboardActions动作,Int32 scanCode,Boolean isExtendedKey,Boolean isSystemKey,Int32 virtualKey)
在System.Windows.Interop。在System.Windows.Interop.HwndSource.CriticalTranslateAccelerator(MSG&msg,ModifierKeys修饰符)中的
$ System.Windows.Interop.HwndSource.OnPreprocessMessage(Object param)
$ h $ d $在System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback,Object args,Boolean isSingleParameter)中的
在System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source,Delegate callback,Object args,Boolean isSingleParameter,代理catchHandler)
InnerException:

但是,当Virtualization设置为False时,它的工作非常正常。



有没有我想念的东西ing?需要一个解决方案。



提前感谢

解决方案

手动或代码插入数据?



如果用手,那么:



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



2)似乎你正在使用工具包版本的datagrid。是最新的(2月10日,是最新的想法)?你是否尝试过.NET 4中的内置数据网格?



如果在代码中:



试图为编辑的项目调用ScrollIntoView,那么该调用必须在datagrid.UpdateLayout()之前。如果然后尝试为您的项目生成一个DataGridRow容器,则在获取DataGridRow之后,您必须先调用UpdateLayout(),然后才能设置任何属性或尝试调用它们之间的方法。
这是非常感谢虚拟化工作的不寻常的方式(至少在datagrid中)。如果你没有像前面提到的那样调用UpdateLayout,那么在某些情况下你会得到(空参考)异常,而不在其他情况下。


I m facing a problem with WPF DataGrid.

I m using WPF DataGrid & DataGridTemplateColumn (textboxes)

Sometimes I get the following error. Whilst inserting data, I get an error off and on. There is no pattern to reproduce this error. It happens randomly.

System.NullReferenceException : Object Reference not set to an instane of an object.

Detailed Message:

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:

But when Virtualization is set to False then it works absolutely fine.

Is there something I'm missing? Need a solution.

Thanks in advance.

解决方案

Do you insert data by hand or via code?

If by hand then:

1) at what point does the exception pop up, when the edited texbox loses focus?

2) seems you are using the "toolkit" version of datagrid. Is it up to date (Feb 10, is the latest I think)? Have you tried out the builtin datagrid in .NET 4?

If in code then:

If you are trying to for instance call ScrollIntoView for the edited item, then that call must be preceded with datagrid.UpdateLayout(). And if you then try to generate a DataGridRow container for your item, then after getting the DataGridRow you must call UpdateLayout() on it as well, before setting any properties or trying to call methods on it. This is all thanks to the unusual way virtualizing works (at least in the datagrid). If you don't call UpdateLayout as I mentioned, you will get (null ref.) exceptions in some scenarios but not in others.

这篇关于wpf数据网格错误/错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆