I have a WPF DataGrid
in a WPF Project Window. I have populated the grid with a DataTable
, and autogenerated the columns (unfortunately a necessity) and have a requirement to change the header colour of the columns dependant on certain other factors.
I have a list of the column names that need to be highlighted, and would easily be able to figure out their indexes based on this (as I generated them myself in the DataGrid
).
However, I can't seem to get the column header to change colour, this has to be done in the code as I don't know at design time which columns will need highlighted. I already have a bit of a template on the header ... not sure if that is "overriding" what I am trying to do.
Grid:
<DataGrid FrozenColumnCount="1" AutoGenerateColumns="True" Grid.Row="1"
AlternationCount="2" HeadersVisibility="Column" Name="dgSkillsMatrix"
Margin="0,0,2,1" HorizontalGridLinesBrush="White" VerticalGridLinesBrush="White"
AlternatingRowBackground="#FFD0D0EB" RowBackground="#FFECECF5" FontSize="10.5"
Grid.ColumnSpan="1" CellStyle="{StaticResource CellHighlighterStyle}"
ColumnHeaderStyle="{StaticResource dataGridColumnHeader}" />
Header template/style:
<DataTemplate x:Key="RotateHeaderTemplate" >
<TextBlock Text="{Binding}" Foreground="Blue" >
<TextBlock.LayoutTransform>
<RotateTransform Angle="-90" />
</TextBlock.LayoutTransform>
</TextBlock>
</DataTemplate>
And this is what I have tried so far to get the column header to change (called on the Window_Activated
event as that is called after the constructor when the grid/WPF tree is actually built):
Style newStyle = new System.Windows.Style()
{
TargetType = typeof(DataGridColumn)
};
// SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#F70F49"))
newStyle.Setters.Add(new Setter(DataGridColumn.HeaderStringFormatProperty, new SolidColorBrush(Colors.Red)));
this.dgSkillsMatrix.Columns[4].HeaderStyle = newStyle;
HeaderStringFormat
toColors.Red
?HeaderStringFormat
is usually something like"M/d/yy"
or"#,##0.00"
, not a color.