R – Is it possible to display a message or default row in a Silverlight 3 Datagrid

datagridsilverlightsilverlight-3.0

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_InsertValue[T](PresentationFrameworkCollection 1 collection, UInt32 index, CValue value)
at MS.Internal.XcpImports.Collection_InsertDependencyObject[T](PresentationFrameworkCollection 1 collection, UInt32 index, DependencyObject value)
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?

Best Answer

I would do this by switching the visibility of the datagrid off and the visibility of say a textblock on. You can do this with binding and a converter:

Converter:

public class ObjectToVisibilityConverter : IValueConverter
{
    public bool Negate { get; set; }

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    if (!Negate)
    {
        return (value == null) ? Visibility.Collapsed : Visibility.Visible;
    }
    else
    {
        return (value == null) ? Visibility.Visible :  Visibility.Collapsed;
    }
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    throw new NotImplementedException();
}

}

XAML:

    <UserControl.Resources>
        <xmlnsref:ObjectToVisibilityConverter x:Key="ObjectToVisibilityConverter" />
        <xmlnsref:ObjectToVisibilityConverter Negate="True" x:Key="ReversedObjectToVisibilityConverter" />
    </UserControl.Resources>
    <StackPanel>
       <data:dataGrid Visibility="{Binding MyDataSetObject, Converter={StaticResource ObjectToVisibilityConverter}}">
       ... />
       <TextBlock Text="No results found."
 Visibility="{Binding MyDataSetObject, Converter={StaticResource ReversedObjectToVisibilityConverter}}"> />
    </StackPanel>

This will hide the grid (and show the textbox) if the MyDataSetObject object is null, and vica-versa if not null (Note the Negate property on the converter that will reverse the visibility)

Related Topic