Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format headers on a WPF Datagrid?

Tags:

wpf

datagrid

I am using the WPF Datagrid from Codeplex.

I am able to style the rows and with the following attributes in the dg:DataGrid element.

But how do I style the Headers? I find 100s of examples on the web which define Styles and use e.g. x:Key="DataGridColumnHeaderStyle" in the Datagrid element, but none of them seem to work for me.

How can I just e.g. change the Datagrid Header background to orange on this DataGrid?

<dg:DataGrid AlternatingRowBackground="#ddd" 
    RowBackground="#eee" 
    Name="theGrid1" 
    VerticalAlignment="Stretch" 
    AutoGenerateColumns="False" 
    BorderBrush="#ddd">
...
</dg:DataGrid>
like image 286
Edward Tanguay Avatar asked Feb 03 '09 12:02

Edward Tanguay


2 Answers

There is also a property on the DataGrid that allows for styling the header:

<DataGrid.ColumnHeaderStyle>
     <Style TargetType="{x:Type DataGridColumnHeader}">
          <Setter Property="FontWeight"
                  Value="Bold" />
     </Style>
</DataGrid.ColumnHeaderStyle>
like image 103
skybluecodeflier Avatar answered Sep 30 '22 12:09

skybluecodeflier


The style in this case is in a file called generic.xaml it should be loacted in a themems folder in your project.

find it and open it. inside you will find this line that controls the background of the column headers

 <dg:DataGridHeaderBorder SortDirection="{TemplateBinding SortDirection}"
                                     IsHovered="{TemplateBinding IsMouseOver}"
                                     IsPressed="{TemplateBinding IsPressed}"
                                     IsClickable="{TemplateBinding CanUserSort}"
                                     Background="{TemplateBinding Background}"
                                     BorderBrush="{TemplateBinding BorderBrush}"
                                     BorderThickness="{TemplateBinding BorderThickness}"
                                     Padding ="{TemplateBinding Padding}"
                                     SeparatorVisibility="{TemplateBinding SeparatorVisibility}"


                         SeparatorBrush="{TemplateBinding SeparatorBrush}">

basically its defined at another place in the template: this will explain TemlateBinding to you MSDN TemplateBinding

HTH, Eric

like image 20
user61477 Avatar answered Sep 30 '22 12:09

user61477