Read-only cells, crash when double-click (causes edit)

Oct 21, 2008 at 8:03 PM
We are generating our DataGridColumns in code based on some outside configuration, and would like all cells to be read-only.

We're creating DataGridTemplateColumns, that has a CellEditingTemplate of null and a Binding mode of OneWay (found those suggestions in another post on this forum).  All is well until I double-click on a cell at runtime.  This apparently causes a DataGrid.BeginEditCommand to get fired, and it then throws an exception saying  'EditItem' is not allowed for this view.  (in DataGrid.EditRowItem calling IEditableCollectionView.EditItem)

I tried intercepting the DataGrid.BeginEditCommand in the containing view to set e.Handled = true, or even set e.CanExecute = false in the CanExecute handler for that command, but it doesn't hit either one. 

How can I turn off this ability to edit a cell via double-click?

Is there any more straight-forward way to make cells read-only without the ability to edit?

Thanks.
Coordinator
Oct 21, 2008 at 9:02 PM

The V1 release of DataGrid would have a IsReadOnly boolean on DataGridColumn which when set to true will disable the editing for all the cells of that column. This feature was not completed for the current release.

It seems that your underlying CollectionView doesn't support editing (based on underlying data object). Hence you will not be able to edit cells of any column. So you can try disabling the editing for all the cells (irrespective of which column they belong too) as follows
    
    <DataGrid>
        <DataGrid.Resources>
            <Style TargetType="{x:Type toolkit:DataGridCell}">
                <Setter Property="IsReadOnly" Value="True"/>
            </Style>
        </DataGrid.Resources>
    </DataGrid>

But if you can wait till V1, it would be much simpler.

Oct 22, 2008 at 8:29 PM
You could also provide your own templates for the columns.  In the case below the TextBlock has no edit behavior so the events are never called and there is no chance of an exception:

                        <dg:DataGrid AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False"  >
                                <dg:DataGrid.Columns>
                                    <dg:DataGridTemplateColumn Header="User Name" SortMemberPath="UserName" >
                                        <dg:DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding Path=UserName}" Margin="1" />
                                            </DataTemplate>
                                        </dg:DataGridTemplateColumn.CellTemplate>
                                    </dg:DataGridTemplateColumn>
                                <dg:DataGrid.Columns>
                            </dg:DataGrid>

Also, if you find that the CanUserAddRows setting is not working properly (in my case only if the datagrid is inside a DataTemplate), you should check the following thread: http://www.codeplex.com/wpf/Thread/View.aspx?ThreadId=34005