I have a DataGrid which is bound to a PagedCollectionView and the underlying collection may contain no items. When this occurs the DataGrid does not render at all, no column headers or anything, and when the DataGrid is then re-bound to another PagedCollectionView that does contain some items it causes a system error
System.ArgumentException: Value does not fall within the expected range.
at MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData)
at MS.Internal.XcpImports.MethodEx(DependencyObject obj, String name) at MS.Internal.XcpImports.UIElement_UpdateLayout(UIElement element)...
or
Message: Unhandled Error in Silverlight Application
Code: 4004
Category: ManagedRuntimeError
Message: System.ArgumentException: Value does not fall within the expected range.
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.Collection_InsertValueT
at MS.Internal.XcpImports.Collection_InsertDependencyObjectT
at System.Windows.PresentationFrameworkCollection 1.InsertDependencyObject(Int32 index, DependencyObject value)
at System.Windows.Controls.UIElementCollection.InsertInternal(Int32 index, UIElement value)
at System.Windows.PresentationFrameworkCollection 1.Insert(Int32 index, T value)
at System.Windows.Controls.DataGrid.InsertDisplayedColumnHeader(DataGridColumn dataGridColumn)
at System.Windows.Controls.DataGrid.OnInsertedColumn_PreNotification(DataGridColumn insertedColumn)
at System.Windows.Controls.DataGridColumnCollection.InsertItem(Int32 columnIndex, DataGridColumn dataGridColumn)
at System.Collections.ObjectModel.Collection 1.Insert(Int32 index, T item)
at System.Windows.Controls.DataGridColumnCollection.EnsureRowGrouping(Boolean rowGrouping)
at System.Windows.Controls.DataGrid.EnsureRowGroupSpacerColumn()
at System.Windows.Controls.DataGrid.RefreshRows(Boolean recycleRows, Boolean clearRows)
at System.Windows.Controls.DataGrid.RefreshRowsAndColumns(Boolean clearRows)
at System.Windows.Controls.DataGrid.MeasureOverride(Size availableSize)
at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
Line: 54
Char: 13
Code: 0
which I assume is caused because the DataGrid is missing a pointer that it should have (but to be honest I really have no idea as I haven't looked into it).
The system exception is obviously a problem and I'd like it to not happen. But making the UI look nice when there are no items in the collection is a business requirement and I figure that fixing the UI to display something nice when the collection is empty might just give me a work around for the System Exception.
So is it possible to display a message or default row in a Silverlight 3 DataGrid?
I've seen Jonathan Shen's answer but I was wondering if there was an easier/simpler/built in way now days as his answer pre-dates Silverlight 3. I also have an issue with the View having to create, in the example, a Person collection & object. My Views have no knowledge of the ViewModel so to implement Jonathan's solution I would also have to implement a secondary Person within the View - not the end of the world, but it seems a little hacky.
Does anyone have a better solution for displaying something nice when binding a potentially empty collection to a Silverlight DataGrid?