从 Silverlight 5 数据网格中删除行时出错
我有一个 Silverlight 4 (ria 1.0) 应用程序,今天已升级到 Silverlight 5 (ria 1.0 sp2)
现在,当我尝试从数据网格中删除记录时,出现以下错误:
at System.Windows.Controls.DataGrid.OnRemovedElement(Int32 slotDeleted, Object itemDeleted, Boolean isRow) at System.Windows.Controls.DataGrid.RemoveElementAt(Int32 slot, Object item, Boolean isRow) at System.Windows.Controls.DataGrid.RemoveRowAt(Int32 rowIndex, Object item) at System.Windows.Controls.DataGridDataConnection.NotifyingDataSource_CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Windows.Controls.DataGridDataConnection.<WireEvents>b__0(DataGridDataConnection instance, Object source, NotifyCollectionChangedEventArgs eventArgs) at System.Windows.Controls.WeakEventListener`3.OnEvent(TSource source, TEventArgs eventArgs) at System.Windows.Data.PagedCollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) at System.Windows.Data.PagedCollectionView.ProcessRemoveEvent(Object removedItem, Boolean isReplace) at System.Windows.Data.PagedCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args) at System.Windows.Data.PagedCollectionView.<.ctor>b__0(Object sender, NotifyCollectionChangedEventArgs args) at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection`1.RemoveItem(Int32 index) at System.Collections.ObjectModel.Collection`1.Remove(T item) at Allscripts.UECPortal.Client.Modules.PayerpathEnrollmentProfile.ViewModels.CompleteUserInformation.CompleteUserInformationViewModel.deleteUserCommandExcuted(Object parameter) at Microsoft.Practices.Prism.Commands.DelegateCommand`1.<>c__DisplayClass6.<.ctor>b__2(Object o) at Microsoft.Practices.Prism.Commands.DelegateCommandBase.Execute(Object parameter) at Microsoft.Practices.Prism.Commands.DelegateCommandBase.System.Windows.Input.ICommand.Execute(Object parameter) at System.Windows.Controls.Primitives.ButtonBase.ExecuteCommand() at System.Windows.Controls.Primitives.ButtonBase.OnClick() at System.Windows.Controls.Primitives.ToggleButton.OnClick() at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName, UInt32 flags)
我将 DomainContext.EntitySet 包装到已绑定的 ObservableCollection 中到数据网格,所以当我尝试从 ObservableCollection 中删除项目时,我收到此错误。
另外,我尝试将 EntitySet 直接绑定到数据网格,并从 EntitySet 中删除项目,但仍然遇到相同的错误。
I have an Silverlight 4 (ria 1.0) application which was upgraded today to Silverlight 5 (ria 1.0 sp2)
Now I am getting the following error when I am trying to delete record from datagrid:
at System.Windows.Controls.DataGrid.OnRemovedElement(Int32 slotDeleted, Object itemDeleted, Boolean isRow) at System.Windows.Controls.DataGrid.RemoveElementAt(Int32 slot, Object item, Boolean isRow) at System.Windows.Controls.DataGrid.RemoveRowAt(Int32 rowIndex, Object item) at System.Windows.Controls.DataGridDataConnection.NotifyingDataSource_CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Windows.Controls.DataGridDataConnection.<WireEvents>b__0(DataGridDataConnection instance, Object source, NotifyCollectionChangedEventArgs eventArgs) at System.Windows.Controls.WeakEventListener`3.OnEvent(TSource source, TEventArgs eventArgs) at System.Windows.Data.PagedCollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) at System.Windows.Data.PagedCollectionView.ProcessRemoveEvent(Object removedItem, Boolean isReplace) at System.Windows.Data.PagedCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args) at System.Windows.Data.PagedCollectionView.<.ctor>b__0(Object sender, NotifyCollectionChangedEventArgs args) at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at System.Collections.ObjectModel.ObservableCollection`1.RemoveItem(Int32 index) at System.Collections.ObjectModel.Collection`1.Remove(T item) at Allscripts.UECPortal.Client.Modules.PayerpathEnrollmentProfile.ViewModels.CompleteUserInformation.CompleteUserInformationViewModel.deleteUserCommandExcuted(Object parameter) at Microsoft.Practices.Prism.Commands.DelegateCommand`1.<>c__DisplayClass6.<.ctor>b__2(Object o) at Microsoft.Practices.Prism.Commands.DelegateCommandBase.Execute(Object parameter) at Microsoft.Practices.Prism.Commands.DelegateCommandBase.System.Windows.Input.ICommand.Execute(Object parameter) at System.Windows.Controls.Primitives.ButtonBase.ExecuteCommand() at System.Windows.Controls.Primitives.ButtonBase.OnClick() at System.Windows.Controls.Primitives.ToggleButton.OnClick() at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName, UInt32 flags)
I have DomainContext.EntitySet wrapped into ObservableCollection which is bound to datagrid, so when I am trying to delete item from ObservableCollection I am getting this error.
Also I have tried to bind EntitySet directly to datagrid, and remove item from EntitySet, I am still getting the same error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,您应该始终为自定义模板中的所有
[TemplatePart]
定义控件,除非控件的文档表明您不必这样做。这些[TemplatePart]
属性的目的是记录控件可能引用的代码部分。如果控件发现其模板缺少重要部分,则应抛出异常。显然 Silverlight 5 Toolkit 中的 DataGrid 并没有这样做 - 也许 Microsoft 打算让它在没有垂直滚动条的情况下使用?Silverlight 5
DataGrid
类有一个字段_vScrollBar
,它存储从控件模板中读取的垂直滚动条(如果模板中存在垂直滚动条)。在OnRemovedElement
方法中,我可以看到代码读取_vScrollBar.Maximum
属性,而无需先检查_vScrollBar
是否为 null。我怀疑这就是您所看到的 NullReferenceException 抛出的地方。我想说这是 Silverlight 5 DataGrid 中的一个错误:DataGrid 要么应该抱怨模板中没有垂直滚动条,要么应该应付没有。Generally, you should always define controls for all
[TemplatePart]
s in a custom template, unless the documentation for the control says that you don't have to. The intention of these[TemplatePart]
attributes is to document the parts of the code the control may refer to. If a control finds that its template is missing essential parts, it should throw an exception. Evidently the DataGrid in the Silverlight 5 Toolkit doesn't do this - perhaps Microsoft intend for it to be used without a vertical scrollbar?The Silverlight 5
DataGrid
class has a field_vScrollBar
which stores the vertical scrollbar read from the control's template, if there is one in the template at all. In theOnRemovedElement
method, I was able to see that the code reads the_vScrollBar.Maximum
property without first checking whether_vScrollBar
is null. I suspect that this is where the NullReferenceException you're seeing is being thrown from. I would say this was a bug in the Silverlight 5 DataGrid: either the DataGrid should complain about there not being a vertical scrollbar in the template, or it should cope without.我已经解决了一个问题。
问题如下:
- 我们的数据网格有自定义模板
- 我们的模板没有 VerticalScrollbar [TemplatePartAttribute(Name = "VerticalScrollbar", Type = typeof(ScrollBar))]
在删除数据网格的行上尝试重新计算高度。这个过程涉及到VerticalScrollbar(虽然以为它应该是不可见的)。一旦模板中没有滚动条,我就会收到 NullReferenceException。
我将 VerticalScrollbar 添加到数据网格模板中,问题得到解决。
在 Silverlight 4 中一切都工作正常。所以我有一个问题:这是Silverlight 5 datagrid缺陷吗?或者我应该始终在自定义模板中定义所有模板部分?
I have resolved an issue.
The problem was the following:
- our datagrid has custom template
- our template was without VerticalScrollbar [TemplatePartAttribute(Name = "VerticalScrollbar", Type = typeof(ScrollBar))]
On row deleting datagrid tries to recalculate heights. This process involves VerticalScrollbar (even thought it should be invisible). As soon as I hadn't scrollbar in the template, I was getting NullReferenceException.
I added VerticalScrollbar to datagrid template and issue was resolved.
In Silverlight 4 everything was working OK. So I have a question: is this Silverlight 5 datagrid defect? or I should always define all template parts in custom template?