微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WPF DataGrid所选项目NullArgumentException

如何解决WPF DataGrid所选项目NullArgumentException

| 我已经开始从从DelegateCommand链接viewmodel的命令获取上述异常。异常说 {\“值不能为空。\\ r \\ n参数名称:键\”} 堆栈跟踪...
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,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,Object state,Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,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,Boolean& handled)
at MS.Win32.HwndSubclass.dispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback,Delegate catchHandler)
at System.Windows.Threading.dispatcher.InvokeImpl(dispatcherPriority priority,TimeSpan timeout,Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,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,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()结束后立即引发异常... 编辑2: 我相信我已经找到了答案。 在使用两个对象列表(可能是ObservableCollections,也许有区别吗?)时,在工作“开关操作”的示例中,我将对象添加到新列表中,然后将其从列表中删除。旧的。 在引发异常的一个中,我将其添加到集合中,然后从另一个中将其删除。我猜这是因为(如果我的假设是错误的,请输入,如果所有内容都是C#中的引用,我移动了\'pointer \',然后删除了指向旧列表的指针引用)不再存在。我想这是一个菜鸟错误,但我几乎认为我不应该当过事!嗯,至少问题似乎解决了!非常感谢您的投入。     

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。