Campl3r Campl3r - 2 months ago 31
C# Question

DataGrid set single cells readonly

I am looking for a way to disable some of my cells in a WPF DataGrid. Since isReadOnly is not a DependencyProperty, I can not use it.

Is there any other way to do this?

I would like to write something like this:

<DataGridTextColumn IsReadOnly="{Binding Path=Value,Converter={StaticResource ValueToBooleanConverter}}" />


but any way how to do this would be nice, since I don't see a way, but Splitting the data in different
DataGrid's
.

Answer

If you can`t make a column read-only, you can go to a cell level. For example by making Style and adding it to required columns:

 <DataGridTextColumn Binding="{Binding TextProperty}">
      <DataGridTextColumn.CellStyle>
           <Style TargetType="DataGridCell">
               <Setter Property="IsEnabled" 
                       Value="{Binding Path=Value,Converter={StaticResource ValueToBooleanConverter}}" />
           </Style>
       </DataGridTextColumn.CellStyle>
 </DataGridTextColumn>

I made the Style inside column here but of cource it can be moved to resources and referenced in any required columns by key (but you gotta be shure converter is accessible for that style). Cell`s IsReadOnly doesn`t seem to have a setter so I`m using IsEnabled here which is doing the job quite nicely.