How to change column header's background color when using WPF datagrid? Need to modify xaml directly?
Use a style with a setter targeted at DataGridColumnHeader:
<DataGrid>
<DataGrid.Resources>
<Style BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="Blue" />
</Style>
</DataGrid.Resources>
</DataGrid>
Try this:
<windows.Resources>
<LinearGradientBrush x:Key="HeaderBrush" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#FF6B8E95" Offset="0"/>
<GradientStop Color="#FF14A7C1" Offset="1"/>
<GradientStop Color="#FF1E424E" Offset="0.509"/>
<GradientStop Color="#FF1D4855" Offset="0.542"/>
<GradientStop Color="#FF1D4855" Offset="0.542"/>
<GradientStop Color="#FF193A44" Offset="0.526"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="HeaderBorderBrush" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#FF1D1D1D" Offset="0.614"/>
<GradientStop Color="#FF007F96" Offset="0.853"/>
<GradientStop Color="#FF0AEAFA" Offset="1"/>
</LinearGradientBrush>
<Style x:Key="HeaderStyle" TargetType="DataGridColumnHeader">
<Setter Property="Background" Value="{StaticResource HeaderBrush}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="BorderBrush" Value="{StaticResource HeaderBorderBrush}" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="MinWidth" Value="0" />
<Setter Property="MinHeight" Value="30" />
<Setter Property="Cursor" Value="Hand" />
</Style>
</Windows.Resources>
<Grid>
<DataGrid Name="dataGrid1" ColumnHeaderStyle="{StaticResource HeaderStyle}"/>
</Grid>
This is the result:
<DataGridTextColumn Header="ID">
<DataGridTextColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Background" Value="Green"/>
</Style>
</DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With