2010-05-26 58 views
4

我面臨WPF DataGrid的問題。WPF數據網格錯誤/錯誤

我爲使用WPF的DataGrid & DataGridTemplateColumn(文本框)

有時候我碰到下面的錯誤。在插入數據的同時,我也會收到錯誤信息。沒有模式來重現此錯誤。它隨機發生。

System.NullReferenceException:對象 參考不設置到 對象的instane。

詳細信息:

 
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,然後它工作絕對沒問題。

有什麼我失蹤了嗎?需要解決方案。

在此先感謝。

回答

3

你是用手還是通過代碼插入數據?

如果手則:

在什麼時候異常彈出,當編輯texbox失去

1)重點是什麼?

2)似乎你正在使用「工具包」版本的數據網格。它是否是最新的(2月10日,我認爲是最新的)?你有沒有在.NET 4中試用內置的數據網格?

如果代碼,然後:

如果你想爲實例調用ScrollIntoView所編輯的項目,然後該呼叫必須用datagrid.UpdateLayout之前()。如果您嘗試爲您的項目生成DataGridRow容器,那麼在獲取DataGridRow之後,您必須先調用UpdateLayout(),然後再設置任何屬性或嘗試調用其上的方法。 這都歸功於虛擬化作品的不尋常方式(至少在數據網格中)。如果你沒有像我提到的那樣調用UpdateLayout,你會在某些情況下得到(null ref。)異常,但在其他情況下則不會。

+0

datagrid.UpdateLayout()適用於我,謝謝 – torpederos 2013-04-24 12:19:09

+0

感謝您指出虛擬化! – Mafii 2016-11-14 13:23:31