DataGridRow IsReadOnly Feature

Dec 19, 2008 at 9:40 AM
Edited Dec 19, 2008 at 9:43 AM

Some data grids will be read only as a whole, some columns might be read only. However some of my data grids have rows that need to be read only. This is not supported in the October 2008 release of the WPFToolBox.

Since there was no work around for my problem, I modified the code Sources.

The IsReadOnly behavior of the cell is now in (DataGridCell.cs):

private static object OnCoerceIsReadOnly(DependencyObject d, object baseValue)
{
    var cell = d as DataGridCell;
    var row = cell.RowOwner;
    var column = cell.Column;
    var dataGrid = cell.DataGridOwner;
    return DataGridHelper.GetCoercedTransferPropertyValue(
        cell,
        baseValue,
        IsReadOnlyProperty,
        row,
        DataGridRow.IsReadOnlyProperty,

        column,  
        DataGridColumn.IsReadOnlyProperty,
        dataGrid,
        DataGrid.IsReadOnlyProperty);
}

Of course we needed to add the DataGridColumn.IsReadOnlyProperty in (DataGridColumn.cs), create a backing property etc. For more details I refer to the attached code.

The last change was made to DataGridHelper in (DataGridHelper.cs), there was no overloaded method yet for GetCoercedTransferPropertyValue with 4 levels available.

How to use it?

A seriously abbreviated code sample would be:

<wpftoolkit:DataGrid>
    <wpftoolkit:DataGrid.RowStyle>
        <Style TargetType="{x:Type wpftoolkit:DataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Locked}" Value="true">
                    <Setter Property="IsReadOnly" Value="true" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </wpftoolkit:DataGrid.RowStyle>
</wpftoolkit:DataGrid>

As shown in this snippet, we can now employ a nice data trigger on the row data item to make the row read only when we want it to be.

Can someone let me know if my efforts are usable?

Cheers,
   Jan Izaak

Dec 19, 2008 at 11:31 AM
By no means do I mean to belittle your efforts, but did you look into implementing a BeginningEdit event, in which you can set e.Cancel to true to prevent the row from entering edit mode?
Dec 19, 2008 at 6:14 PM
Thats a very good suggestion and i confess i did not try that yet.

However, I now have generic styles for all my columns that show a different cell style when IsReadOnly is true (to indicate to the user that it cannot be editted). In other words my read-only columns and read-only row cells use the same style in all my grids.

I'm also trying to have a pure XAML user interface that is mostly data driven. 
Oct 7, 2010 at 8:32 AM

DataGridRow isreadonly property, does it work with latest .net wpf datagrid?