更新DataGrid时mscorlib.dll中未处理的异常argumentnullexception

时间:2016-02-24 08:54:11

标签: c# .net wpf wpfdatagrid

所以我有一个wpf应用程序,它有一个datagrid来显示数据库中的项目。我想刷新项目,所以我已经实现了一个刷新方法来替换需要刷新的项目。

Datagrid itemssource绑定到ICollectionView,该ICollectionView具有以项目为源的observablecollection。

看起来像这样:

private async Task RefreshContentOrders()
{   
    _isRefreshingContentOrders = true;
    var orders = await _contentOrderSession.GetAllContentOrdersAvaliableToUser(CurrentUser);
    foreach (var order in orders)
    {
        var currentItem = Orders.FirstOrDefault(x =>x!=null&& x.Id == order.Id);
        if (currentItem == null)
            Orders.Add(currentItem);
        else
        {
        Orders.Remove(currentItem);
        Orders.Add(order);                 
        }
    }    
    _isRefreshingContentOrders = false;
}

只要没有新项目,我就可以毫无问题地刷新我的列表。 如果我在列表中添加一个新项目并使用此方法刷新,当我向下滚动以查看新项目时,我的应用程序会在mscorlib.dll中引发异常。显示以下消息和堆栈跟踪:

消息: {"值不能为空。\ r \ nParametername:key"}

堆栈跟踪:

   vid System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
   vid System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
   vid System.Windows.Controls.DataGridItemAttachedStorage.TryGetValue(Object item, DependencyProperty property, Object& value)
   vid System.Windows.Controls.DataGridRow.RestoreAttachedItemValue(DependencyObject objectWithProperty, DependencyProperty property)
   vid System.Windows.Controls.DataGridRow.SyncProperties(Boolean forcePrepareCells)
   vid System.Windows.Controls.DataGridRow.PrepareRow(Object item, DataGrid owningDataGrid)
   vid System.Windows.Controls.DataGrid.PrepareContainerForItemOverride(DependencyObject element, Object item)
   vid System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(DependencyObject container, Object item)
   vid System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(DependencyObject container)
   vid System.Windows.Controls.VirtualizingStackPanel.InsertContainer(Int32 childIndex, UIElement container, Boolean isRecycled)
   vid System.Windows.Controls.VirtualizingStackPanel.AddContainerFromGenerator(Int32 childIndex, UIElement child, Boolean newlyRealized, Boolean isBeforeViewport)
   vid System.Windows.Controls.VirtualizingStackPanel.MeasureChild(IItemContainerGenerator& generator, IContainItemStorage& itemStorageProvider, IContainItemStorage& parentItemStorageProvider, Object& parentItem, Boolean& hasUniformOrAverageContainerSizeBeenSet, Double& computedUniformOrAverageContainerSize, Double& computedUniformOrAverageContainerPixelSize, Boolean& computedAreContainersUniformlySized, IList& items, Object& item, IList& children, Int32& childIndex, Boolean& visualOrderChanged, Boolean& isHorizontal, Size& childConstraint, Rect& viewport, VirtualizationCacheLength& cacheSize, VirtualizationCacheLengthUnit& cacheUnit, Boolean& foundFirstItemInViewport, Double& firstItemInViewportOffset, Size& stackPixelSize, Size& stackPixelSizeInViewport, Size& stackPixelSizeInCacheBeforeViewport, Size& stackPixelSizeInCacheAfterViewport, Size& stackLogicalSize, Size& stackLogicalSizeInViewport, Size& stackLogicalSizeInCacheBeforeViewport, Size& stackLogicalSizeInCacheAfterViewport, Boolean& mustDisableVirtualization, Boolean isBeforeFirstItem, Boolean isAfterFirstItem, Boolean isAfterLastItem, Boolean skipActualMeasure, Boolean skipGeneration, Boolean& hasBringIntoViewContainerBeenMeasured, Boolean& hasVirtualizingChildren)
   vid System.Windows.Controls.VirtualizingStackPanel.MeasureOverrideImpl(Size constraint, Nullable`1& lastPageSafeOffset, List`1& previouslyMeasuredOffsets, Nullable`1& lastPagePixelSize, Boolean remeasure)
   vid System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)
   vid System.Windows.Controls.Primitives.DataGridRowsPresenter.MeasureOverride(Size constraint)
   vid System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   vid System.Windows.UIElement.Measure(Size availableSize)
   vid System.Windows.ContextLayoutManager.UpdateLayout()
   vid System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   vid System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
   vid System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   vid System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   vid System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   vid System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   vid System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   vid System.Windows.Threading.DispatcherOperation.InvokeImpl()
   vid System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   vid System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   vid System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   vid System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   vid System.Windows.Threading.DispatcherOperation.Invoke()
   vid System.Windows.Threading.Dispatcher.ProcessQueue()
   vid System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   vid MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   vid MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   vid System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   vid System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   vid System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   vid MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   vid MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   vid System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   vid System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   vid System.Windows.Application.RunDispatcher(Object ignore)
   vid System.Windows.Application.RunInternal(Window window)
   vid System.Windows.Application.Run(Window window)
   vid System.Windows.Application.Run()
   vid White.Db.ContentOrderDb.App.Main() i C:\Revit Dev\GENERELLA\DATABASER\ContentOrderDb\White.Db.ContentOrderDb.Application\obj\Debug\App.g.cs:rad 0
   vid System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   vid System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   vid Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   vid System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   vid System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   vid System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   vid System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   vid System.Threading.ThreadHelper.ThreadStart()

如果我改为重新创建ObservableCollection和ICollectionView一切正常。

当我重新启动应用程序时,我添加的新项目就在那里,一切看起来都很正常。

我现在已经试图弄清楚什么是错的,但我似乎无法弄明白。我在visual studio 2015中运行.Net 4.6.1。

很明显,它与stacktrace中的datagrid有关,但我很难确定它不喜欢我的代码。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

看起来你在这里有错误:     if(currentItem == null)             Orders.Add(CURRENTITEM);

答案 1 :(得分:0)

没有看到更多的代码我不能确定,但​​我认为Orders是你的ObservableCollection?如果是这样,我认为您可能会尝试从与UI线程不同的线程更新它。

你可以尝试看到的是在你的构造函数中添加它或者你在哪里创建ObservableCollection。

BindingOperations.EnableCollectionSynchronization(Orders, Orders);

它允许您从与UI线程不同的线程修改集合。