Search code examples
wpfwpfdatagrid

DataGridCloumnHeaderCheckbox style


I am using WPF datagrid. Using the following style I am applying checkbox to one of the columheader.

<Style x:Key="TestDataColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                            <CheckBox x:Name="chkbxCheckAll" VerticalAlignment="Center" HorizontalAlignment="Center" IsChecked="{Binding Path=CheckAll, Mode=TwoWay, ElementName=TestDataScreen}" Click="chkbxCheckAll_Click">

                            </CheckBox>
        </ControlTemplate>
    </Setter.Value>
</Setter>
<Setter Property="Control.Background">
    <Setter.Value>
        <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
            <GradientStop Offset="1" Color="#FFC2E3F6" />
            <GradientStop Offset="0.53" Color="#FFF1FBFD" />
            <GradientStop Color="#FFF2FAFD" />
        </LinearGradientBrush>
    </Setter.Value>
</Setter>
<Setter Property="Control.BorderBrush" Value="#FFDADADA" />
<Setter Property="Control.BorderThickness" Value="1,0,1,1" />
<Setter Property="Control.Height" Value="26" />
<Setter Property="Control.HorizontalContentAlignment" Value="Center"/>
</Style>

The problem I am facing is, I can see the checkbox in columnheader but columnheader background color is not changed.


Solution

  • Try adding a border around the checkbox like this

    <Border Background="{TemplateBinding Background}">
      <CheckBox x:Name="chkbxCheckAll" VerticalAlignment="Center"
              HorizontalAlignment="Center" 
              IsChecked="{Binding Path=CheckAll, Mode=TwoWay, ElementName=TestDataScreen}" 
              Click="chkbxCheckAll_Click">
      </CheckBox>
    </Border>
    

    Edit:

    If you make the background black and set the background of the CheckBox to TemplateBinding Background you will notice that a littlebit of the checkbox is getting black. This has to do with the build-in style of the CheckBox.