1
votes

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.

2
What exception message are you getting? You've given us a stacktrace, but what was the exception at the top of the stacktrace (e.g. NullReferenceException, ArgumentException, etc.) and what was the message it contained?Luke Woodward
I am getting System.NullReferenceException : Object reference not set to an instance of an object.Vasyl Bodak

2 Answers

2
votes

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 the OnRemovedElement 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.

1
votes

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?