Search code examples
c#wpfxamlmvvm

Hide Checkbox in DatagridCheckBoxColumn in certain Cells


I have a Datagrid with a DatagridCheckboxColumn. I want only selected Rows to show the Checkbox. I tried it with Visibility but then the cells are -obviously- gone. So you can't klick them and they don't have the same background-colour.

Is there any possibility to do this with Binding ?

Datagrid with hidden Checkboxes

Datagrid with hidden Checkboxes

Here is my xaml Code:

<DataGrid   Name="dtgDecodedMsg"
        CanUserSortColumns="False"
        CanUserAddRows="False"
        CanUserReorderColumns="False"
        HeadersVisibility="Column"
        IsTabStop="False"
        ClipboardCopyMode="IncludeHeader"
        SelectedIndex="{Binding DecodeSelectedGridIdx, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
        ItemsSource="{Binding Path=MsgTypGridLineListVar, UpdateSourceTrigger=PropertyChanged}" 
        AutoGenerateColumns="False" 
        Margin="10,111,10,0">
        <DataGrid.Columns>
          <DataGridCheckBoxColumn Header="..." Width="25">
            <DataGridCheckBoxColumn.CellStyle>
              <Style TargetType="{x:Type DataGridCell}">
                <Style.Triggers>
                  <Trigger Property="IsSelected" Value="True">
                    <Setter Property="BorderBrush" Value="LightBlue"/>
                    <Setter Property="BorderThickness" Value="2"/>
                  </Trigger>
                </Style.Triggers>
                <Setter Property="Background" Value="{Binding HideCell.CellColor}"/>
                <Setter Property="BorderBrush" Value="{Binding HideCell.CellColor}"/>
                <Setter Property="Visibility" Value="{Binding HideCell.CellVisibility}"/>
              </Style>
            </DataGridCheckBoxColumn.CellStyle>
          </DataGridCheckBoxColumn>
          <DataGridTextColumn Header="Value" Binding="{Binding ValueTelegramCell.CellValue}" IsReadOnly="True" Width="*">
            <DataGridTextColumn.CellStyle>
              <Style TargetType="{x:Type DataGridCell}">
                <Style.Triggers>
                  <Trigger Property="IsSelected" Value="True">
                    <Setter Property="BorderBrush" Value="LightBlue"/>
                    <Setter Property="BorderThickness" Value="2"/>
                  </Trigger>
                </Style.Triggers>
                <Setter Property="Foreground" Value="{Binding ValueTelegramCell.TextColor}"/>
                <Setter Property="Background" Value="{Binding ValueTelegramCell.CellColor}"/>
                <Setter Property="BorderBrush" Value="{Binding ValueTelegramCell.CellColor}"/>
              </Style>
            </DataGridTextColumn.CellStyle>
          </DataGridTextColumn>
        </DataGrid.Columns>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="Visibility" Value="{Binding RowVisible , Mode=OneWay}"/>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

So atm I am binding the Visibility of the Cell, but that doesn't give the results I want.. See:

<Setter Property="Visibility" Value="{Binding HideCell.CellVisibility}"/>

It should look like this: (With the Cell clickable to select to whole Row)

Datagrid how it should be

Datagrid how it should be

Thank you!


Solution

  • i got a Solution that works for me, if anyone has the same problem here is the answer:

    I now used Elementstyle to directly get access to the Checkbox.

                  <DataGridCheckBoxColumn Header="..." Width="25">
                <DataGridCheckBoxColumn.CellStyle>
                  <Style TargetType="{x:Type DataGridCell}">
                    <Style.Triggers>
                      <Trigger Property="IsSelected" Value="True">
                        <Setter Property="BorderBrush" Value="LightBlue"/>
                        <Setter Property="BorderThickness" Value="2"/>
                      </Trigger>
                    </Style.Triggers>
                    <Setter Property="Background" Value="{Binding HideCell.CellColor}"/>
                    <Setter Property="BorderBrush" Value="{Binding HideCell.CellColor}"/>
                    <Setter Property="Focusable" Value="{Binding HideCell.CheckBoxEnabled}"/>
                  </Style>
                </DataGridCheckBoxColumn.CellStyle>
                <DataGridCheckBoxColumn.ElementStyle>
                  <Style TargetType="CheckBox">
                    <Setter Property="Visibility" Value="{Binding HideCell.CellVisibility}"/>
                  </Style>
                </DataGridCheckBoxColumn.ElementStyle>
              </DataGridCheckBoxColumn>