Reputation: 2547
I have started getting the above exception from a command linked from a DelegateCommand to a ViewModel. The exception says
{"Value cannot be null.\r\nParameter name: key"}
StackTrace...
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
at System.Windows.Controls.DataGridItemAttachedStorage.TryGetValue(Object item, DependencyProperty property, Object& value)
at System.Windows.Controls.DataGridRow.RestoreAttachedItemValue(DependencyObject objectWithProperty, DependencyProperty property)
at System.Windows.Controls.DataGridRow.SyncProperties(Boolean forcePrepareCells)
at System.Windows.Controls.DataGridRow.PrepareRow(Object item, DataGrid owningDataGrid)
at System.Windows.Controls.DataGrid.PrepareContainerForItemOverride(DependencyObject element, Object item)
at System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(DependencyObject container, Object item)
at System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(DependencyObject container)
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.MeasureOverride(Size constraint)
at System.Windows.Controls.Primitives.DataGridRowsPresenter.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.ContextLayoutManager.UpdateLayout()
at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
The funny thing is I have a command 'wired' from the View to the ViewModel in just the same way and it works fine.
I'd include the offending code, but the intellisense breaks out at 'No Source Available'. Where is this coming from and how can I get rid of it?!
EDIT: XAML:
<Grid Grid.Column="0" Grid.Row="0">
<DataGrid ItemsSource="{Binding Path=ObservableCollectionOfActiveObjects}" SelectedItem="{Binding Path=SelectedObject}" IsEnabled="{Binding Path=IsDoingNothing}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" Header="ID"/>
<DataGridTextColumn Binding="{Binding Name}" Header="Object Name"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
ViewModel
private void DoDataGridCommand()
{
if (DoHaveAllTheCollections() &&
selectedObject != null &&
!otherListOfObject.Contains(selectedObject))
{
activeObjects.Remove(selectedObject);
excludedObjects.Add(selectedObject);
RaisePropertyChanged(activeObjectsPropertyName);
RaisePropertyChanged(excludedObjectsPropertyName);
}
}
I hope this helps.... the exception throws just after the DoDataGridCommand() ends...
EDIT2:
I believe I have found the answer.
While playing with the two lists of objects (happened to be ObservableCollections, maybe this makes a difference?), in the example of the 'switch operation' that worked I was adding the object to the new list and then removing it from the old one.
In the one that threw the exception, I was adding it to the collection and then removing it from the other. I guess this is because (and please input would be appreciated if my assumption is wrong), as everything is a reference in C#, I moved the 'pointer' then removed the pointers reference to the old list, when, I guess it didn't exist anymore. I suppose it's a rookie mistake, but I hardly think I deserve demerits! Ah well, at least the problem appears solved! Many thanks for your inputs.
Upvotes: 0
Views: 3542
Reputation: 50672
Could you check the data that is being bound to the grid? It looks a bit like the DataGrid is looking for a column/property in the rows that doesn't exist.
Upvotes: 2