WPF DataGrid所选项NullArgumentException

时间:2011-06-14 14:17:13

标签: c# wpf mvvm datagrid delegates

我已经从从DelegateCommand链接到ViewModel的命令开始获取上述异常。例外是

{“值不能为空。\ r \ nParameter name:key”}

...栈跟踪

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()

有趣的是我从View到ViewModel的命令'有线'的方式也一样,而且工作正常。

我会包含有问题的代码,但智能感知会在“无源可用”时爆发。这是从哪里来的,我怎么能摆脱它?!

编辑: 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>

视图模型

        private void DoDataGridCommand()
        {
            if (DoHaveAllTheCollections() &&
                selectedObject != null &&
                !otherListOfObject.Contains(selectedObject))
            {
                activeObjects.Remove(selectedObject);
                excludedObjects.Add(selectedObject);

                RaisePropertyChanged(activeObjectsPropertyName);
                RaisePropertyChanged(excludedObjectsPropertyName);
            } 
        }

我希望这有帮助......在DoDataGridCommand()结束后抛出异常......

EDIT2:

我相信我找到了答案。

在玩两个对象列表时(碰巧是ObservableCollections,也许这有所不同?),在“切换操作”的例子中,我将对象添加到新列表然后将其从旧的。

在抛出异常的那个中,我将它添加到集合中,然后将其从另一个中删除。我想这是因为(并且如果我的假设是错误的,请输入,因为一切都是C#中的引用,我移动了'指针'然后删除了指向旧列表的指针,当时,我猜它没有' t再也存在了。我想这是一个菜鸟的错误,但我认为我不应该记过这个错误!好吧,至少问题出现了解决!非常感谢您的投入。

1 个答案:

答案 0 :(得分:2)

您可以检查绑定到网格的数据吗?看起来有点像DataGrid正在寻找不存在的行中的列/属性。