【问题标题】:Error when deleting row from Silverlight 5 datagrid从 Silverlight 5 数据网格中删除行时出错
【发布时间】:2012-01-25 19:41:24
【问题描述】:

我有一个 Silverlight 4 (ria 1.0) 应用程序,今天升级到 Silverlight 5 (ria 1.0 sp2)

现在当我尝试从数据网格中删除记录时出现以下错误:

在 System.Windows.Controls.DataGrid.OnRemovedElement(Int32 slotDeleted,对象 itemDeleted,布尔 isRow) 在 System.Windows.Controls.DataGrid.RemoveElementAt(Int32 插槽,对象项,布尔 isRow) 在 System.Windows.Controls.DataGrid.RemoveRowAt(Int32 rowIndex,对象项) 在 System.Windows.Controls.DataGridDataConnection.NotifyingDataSource_CollectionChanged(对象发件人,NotifyCollectionChangedEventArgs e) 在 System.Windows.Controls.DataGridDataConnection.b__0(DataGridDataConnection 实例,对象源,NotifyCollectionChangedEventArgs eventArgs) 在 System.Windows.Controls.WeakEventListener`3.OnEvent(TSource 源,TEventArgs eventArgs) 在 System.Windows.Data.PagedCollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs 参数) 在 System.Windows.Data.PagedCollectionView.ProcessRemoveEvent(对象已删除项目,布尔 isReplace) 在 System.Windows.Data.PagedCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs 参数) 在 System.Windows.Data.PagedCollectionView.<.ctor>b__0(对象发送者,NotifyCollectionChangedEventArgs 参数) 在 System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) 在 System.Collections.ObjectModel.ObservableCollection`1.RemoveItem(Int32 索引) 在 System.Collections.ObjectModel.Collection`1.Remove(T 项) 在 Allscripts.UECPortal.Client.Modules.PayerpathEnrollmentProfile.ViewModels.CompleteUserInformation.CompleteUserInformationViewModel.deleteUserCommandExcuted(对象参数) 在 Microsoft.Practices.Prism.Commands.DelegateCommand`1.c__DisplayClass6.<.ctor>b__2(Object o) 在 Microsoft.Practices.Prism.Commands.DelegateCommandBase.Execute(对象参数) 在 Microsoft.Practices.Prism.Commands.DelegateCommandBase.System.Windows.Input.ICommand.Execute(对象参数) 在 System.Windows.Controls.Primitives.ButtonBase.ExecuteCommand() 在 System.Windows.Controls.Primitives.ButtonBase.OnClick() 在 System.Windows.Controls.Primitives.ToggleButton.OnClick() 在 System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) 在 System.Windows.Controls.Control.OnMouseLeftButtonUp(控制 ctrl,EventArgs e) 在 MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj,IntPtr unmanagedObjArgs,Int32 argsTypeIndex,Int32 actualArgsTypeIndex,字符串 eventName,UInt32 标志)

我将 DomainContext.EntitySet 包装到绑定到数据网格的 ObservableCollection 中,因此当我尝试从 ObservableCollection 中删除项目时出现此错误。

我也尝试将 EntitySet 直接绑定到数据网格,并从 EntitySet 中删除项目,但我仍然遇到同样的错误。

【问题讨论】:

  • 您收到什么异常消息?您给了我们一个堆栈跟踪,但堆栈跟踪顶部的异常是什么(例如 NullReferenceException、ArgumentException 等)以及它包含的消息是什么?
  • 我收到 System.NullReferenceException : 对象引用未设置为对象的实例。

标签: silverlight datagrid wcf-ria-services observablecollection entityset


【解决方案1】:

通常,您应该始终在自定义模板中为所有[TemplatePart]s 定义控件,除非该控件的文档说明您不必这样做。这些[TemplatePart] 属性的目的是记录控件可能引用的代码部分。如果一个控件发现它的模板缺少必要的部分,它应该抛出一个异常。显然 Silverlight 5 Toolkit 中的 DataGrid 没有这样做 - 也许微软打算在没有垂直滚动条的情况下使用它?

Silverlight 5 DataGrid 类有一个字段_vScrollBar,它存储从控件模板读取的垂直滚动条(如果模板中有的话)。在OnRemovedElement 方法中,我能够看到代码读取_vScrollBar.Maximum 属性,而无需先检查_vScrollBar 是否为空。我怀疑这是您看到的 NullReferenceException 被抛出的地方。我会说这是 Silverlight 5 DataGrid 中的一个错误:DataGrid 应该抱怨模板中没有垂直滚动条,或者它应该没有。

【讨论】:

  • 感谢您的准确解释。希望有人将此归档为 Connect 问题。
【解决方案2】:

我已经解决了一个问题。

问题如下: - 我们的数据网格有自定义模板 - 我们的模板没有 VerticalScrollbar [TemplatePartAttribute(Name = "VerticalScrollbar", Type = typeof(ScrollBar))]

在行删除数据网格尝试重新计算高度。这个过程涉及到 VerticalScrollbar(甚至认为它应该是不可见的)。一旦我在模板中没有滚动条,我就会得到 NullReferenceException。 我将 VerticalScrollbar 添加到 datagrid 模板并解决了问题。

在 Silverlight 4 中一切正常。所以我有一个问题:这是 Silverlight 5 数据网格缺陷吗?或者我应该始终在自定义模板中定义所有模板部分?

【讨论】:

    猜你喜欢
    • 2012-06-06
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多