Home > Blockchain >  How to set DataGrid column property same as binding value?
How to set DataGrid column property same as binding value?

Time:04-02

How come I can set my binding for my DataGrid like this:

 <DataGridCheckBoxColumn Header="Include" Binding="{Binding isSelected}"/>

But when I add the "IsReadOnly"

<DataGridCheckBoxColumn Header="Include" IsReadOnly="{Binding isSelected}" Binding="{Binding isSelected}"/>

it gives me an warning on "IsReadOnly" that no datacontext is found.

CodePudding user response:

IsReadOnly refers to all items, so the corresponding data context is one level highter than Binding, which refers to the specific objects in your table. So you need to define the cell template manually like this to disable some cells specifically:

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <Grid>
            <CheckBox IsChecked="{Binding isSelected, Mode=TwoWay}"
                      IsEnabled="{Binding isSelected}"/>
        </Grid>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

See also this question, which basically describes the same problem.

CodePudding user response:

This might be the issue with Element style. Whether this element can possibly be returned as a hit test result from some portion of its rendered content.

Try adding ElementStyle element to your DataGridCheckBoxColumn

 <DataGridCheckBoxColumn Header="Include" IsReadOnly="{Binding isSelected}"  ElementStyle="{StaticResource ReadOnlyCheckBoxStyle}" Binding="{Binding isSelected}"/>

And the following style

<Style x:Key="ReadOnlyCheckBoxStyle" TargetType="{x:Type CheckBox}">
      <Setter Property="IsHitTestVisible" Value="False"/> 
</Style>
  • Related