So I'm trying to create a table that has a lot of different formats. The thing I'm currently struggling with is how to adjust the color of a column header in a Datagrid in WPF by coding in the XAML. All I have found is ways to format all of the column headers. Is there any way to format each one separately?
My code currently looks like this:
<DataGrid x:Name="CellStyleGrid" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="309,50,0,0" IsReadOnly="False" IsEnabled="True" CanUserAddRows="True" Width="172" RenderTransformOrigin="0.501,0.477" SelectionUnit="Cell" Grid.ColumnSpan="2" AlternatingRowBackground="#C2C2C2" AlternationCount="2" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=col1}" ClipboardContentBinding="{x:Null}" Header="Col1" CanUserResize="True"/>
<DataGridTextColumn Binding="{Binding Path=col2}" ClipboardContentBinding="{x:Null}" Header="Col2" CanUserResize="True"/>
<DataGridTextColumn Binding="{Binding Path=col3}" ClipboardContentBinding="{x:Null}" Header="Col3" CanUserResize="True"/>
</DataGrid.Columns>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="#3B3B3B"/>
<Setter Property="Foreground" Value="White"/>
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
This code creates a simple table with three columns and colors the headers a dark gray with white foreground text. I am looking to make one of the headers a lighter gray. Thanks! Jon
Set AutoGenerateColumns to false, then in each column definition specify the DataGridXColumn.HeaderStyle. Here is a rough example:
<DataGrid AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{SomeBinding}">
<DataGridTextColumn.HeaderStyle>
<Style>
<Setter Property="InkCanvas.Background"
Value="Blue" />
</Style>
</DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>