WPF DataGrid所选项目为NullArgumentException

本文关键字:NullArgumentException 项目 选项 DataGrid WPF | 更新日期: 2023-09-27 17:59:11

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

{"值不能为null。''r''n参数名称: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()

有趣的是,我有一个命令以同样的方式从View"连接"到ViewModel,它工作得很好。

我会包含有问题的代码,但intellisense在"No Source Available"处爆发。这是从哪里来的?我该如何摆脱它?!

编辑: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);
            } 
        }

我希望这能有所帮助。。。。该异常在DoDataGridCommand()结束后立即抛出。。。

第2版:

我相信我已经找到了答案。

在处理两个对象列表(碰巧是ObservableCollections,也许这有区别?)时,在"切换操作"的例子中,我将对象添加到新列表中,然后将其从旧列表中删除。

在引发异常的一个中,我将其添加到集合中,然后从另一个中删除。我想这是因为(如果我的假设是错误的,请输入),由于C#中的所有内容都是引用,我移动了"指针",然后删除了对旧列表的指针引用,我想它已经不存在了。我想这是一个新手犯的错误,但我认为我不应该被记过!啊,好吧,至少问题看起来解决了!非常感谢您的投入。

WPF DataGrid所选项目为NullArgumentException

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

相关文章:
  • 没有找到相关文章