conditional cell IsReadOnly

Apr 30, 2009 at 12:12 AM
Hi,

I'm trying to do something I would have thought was pretty straight-forward.  I have some cells within my grid which I want to have disabled.  Within each of the business objects which make-up the itemsSource for the grid, I have a property available which will indicate whether the cell should be read-only.  I've tried the following things:

-In the code-behind, I handle the CellEditEnding event, and I tried calling CancelEdit() method on my business object (if the cell should be readonly, that is).  I figure this way, I can let the user try entering info, and then give them some sort of helpful msg, saying something like "you can't edit this value b/c reason XYZ."  However, when I did this, CancelEdit() did not refresh the value displayed in the cell.  So then I tried manually refreshing the grid after CancelEdit(), and I got the following message: "'Refresh' is not allowed during an AddNew or EditItem transaction."

-Instead of handling the edit after-the-fact (as this could lead to some user frustration if they've been typing away, and then the app is telling them that their work is no good), I next tried conditionally disabling the cell to preempt any confusion.  I added a new cell style for the column which I want to have this conditional-cell-disable behavior:

<dg:DataGridTextColumn.CellStyle>
      <Style  TargetType="{x:Type dg:DataGridCell}">
            <Style.Triggers>
                  <DataTrigger Binding="{Binding Path=AllowEdit}" Value="False">
                        <Setter  Property="dg:DataGridCell.IsReadOnly" Value="True" />
                   </DataTrigger>
             </Style.Triggers>
     </Style>
</dg:DataGridTextColumn.CellStyle>

but I got a compile error on this (understandably) - saying that DataGridCell's IsReadOnly property is read-only.

-Next, I tried using the same style as before - but set the column's IsReadOnly instead.  The problem here is how to refer to the column.  I tried using the Setter's ElementName  property, but that wasn't allowed.

-Finally, I abandoned the CellStyle, and tried binding the IsReadOnly property of the column to the property on my business object (which would indicate whether the cell should be read-only), but that didn't work either:

<dg:DataGridTextColumn Header="Description" Binding="{Binding Description}" x:Name="colDescription" Width="450" IsReadOnly="{Binding AllowEdit, Mode=OneWay}"/>

 I got a binding error, which made it seem like the source element in the binding was null.

So what's going on here?  Am I missing something simple?  Much thanks in advance for anyone who can shed some light on this issue.
-Matt
Oct 27, 2009 at 7:59 PM

Hi there,

Have you tried using the EditingElementStyle inside your DataGridTextColumn?  For my object, I have an IsNew property that is set to true for the new row.  Here is how I handle the readonly columns in my situation:

<dg:DataGridTextColumn>
    <dg:DataGridTextColumn.CellStyle>
        <Style TargetType="{x:Type dg:DataGridCell}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsNew}" Value="False">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </dg:DataGridTextColumn.CellStyle>
    <dg:DataGridTextColumn.EditingElementStyle>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsNew}" Value="False">
                    <Setter Property="IsReadOnly" Value="True" />
                </DataTrigger>
            </Style.Triggers>                            
        </Style>
    </dg:DataGridTextColumn.EditingElementStyle>
</dg:DataGridTextColumn>