39

I need to align WPF DataGrid Column Header text to Center. I created a style and attached that using the HeaderStyle property as below.

Style

<Window.Resources>
    <Style x:Key="CenterGridHeaderStyle" TargetType="DataGridColumnHeader">
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
    </Style>
</Window.Resources>

Column

<DataGridTextColumn 
   Binding="{Binding Path=Name}" Header="Name" IsReadOnly="True" 
   HeaderStyle="{DynamicResource CenterGridHeaderStyle}"/>

But this does not align Column Header text to the Center. How can I do this?

Bishan
  • 15,211
  • 52
  • 164
  • 258

6 Answers6

82

Check this

<DataGridTextColumn Header="Nombre"
                          Binding="{Binding Nombre}">
<DataGridTextColumn.HeaderStyle>
  <Style TargetType="DataGridColumnHeader">
     <Setter Property="HorizontalContentAlignment"
                 Value="Center" />
  </Style>
</DataGridTextColumn.HeaderStyle>
Anjali
  • 1,680
  • 4
  • 26
  • 48
  • 3
    Would be appreciated any comment why Your solution does work and not solution of @Bishan. – Tatranskymedved Dec 12 '16 at 15:24
  • 2
    I think we can be happy that at WPF is not everything of the type object() and we need to add every single property via styles and bindings. What a laborious f.... – marsh-wiggle Jun 05 '19 at 20:14
18

It should be StaticResource instead of DynamicResource in the Column:

Style

<Window.Resources>
    <Style x:Key="CenterGridHeaderStyle" TargetType="DataGridColumnHeader">
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
    </Style>
</Window.Resources>

Column

<DataGridTextColumn 
   Binding="{Binding Path=Name}" Header="Name" IsReadOnly="True" 
   HeaderStyle="{StaticResource CenterGridHeaderStyle}"/>
Evgeniy Lyakh
  • 181
  • 1
  • 5
  • 3
    IMO this is the actual correct answer. The accepted answer provides an alternative but does not actually say why the code provided in the question is wrong. – DMX David Cardinal Sep 30 '20 at 18:36
2

There is a response for doing it programmatically at AutoGeneratingColumn:

 private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
                e.Column.HeaderStyle = new Style(typeof(DataGridColumnHeader));
                e.Column.HeaderStyle.Setters.Add(new Setter(HorizontalContentAlignmentProperty, HorizontalAlignment.Center));

    }

Tip, use attributes:

public class ColumnNameAttribute : Attribute
{
    public HorizontalAlignment Alignment { get; set;}
    public ColumnNameAttribute(HorizontalAlignment alignment){
        Alignment = alignment;
}

public class Example(){
    [ColumnName(HorizontalAlignment.Center)]
    public string Column {get; set;}
}

 private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
   var desc = e.PropertyDescriptor as PropertyDescriptor;
   var att = desc.Attributes[typeof(ColumnNameAttribute)] as ColumnNameAttribute;
   if(att != null){
           e.Column.HeaderStyle = new Style(typeof(DataGridColumnHeader));
           e.Column.HeaderStyle.Setters.Add(new Setter(HorizontalContentAlignmentProperty, att.Alignment));
    }

}
David Soler
  • 192
  • 1
  • 10
0

Try this

<DataGridTextColumn.CellStyle>
  <Style>
    <Setter Property="HorizontalAlignment" Value="Center" />
  </Style>
</DataGridTextColumn.CellStyle>
Rushi Soni
  • 1,088
  • 1
  • 13
  • 21
0

I landed here while searching for the same problem for Row Headers alignment. In case anyone else was searching, the solution is as simple as:

<DataGrid.RowHeaderStyle>
  <Style TargetType="DataGridRowHeader">
    <Style.Resources>
      <Style TargetType="StackPanel">
        <Setter Property="HorizontalAlignment" Value="Center" />
      </Style>
    </Style.Resources>
  </Style>
</DataGrid.RowHeaderStyle>
dotNET
  • 33,414
  • 24
  • 162
  • 251
0

Here is what I am using to change the header text alignment.

<DataGrid.Columns>
    <DataGridTemplateColumn Width="Auto" MinWidth="60" Header=" ID " IsReadOnly="True">                                                                <DataGridTemplateColumn.HeaderStyle>
     <Style TargetType="DataGridColumnHeader">
         <Setter Property="HorizontalContentAlignment" Value="Center"/>
         <Setter Property="Background"  Value="#c0c0c0"/>
         <Setter Property="BorderThickness" Value="1"/>
         <Setter Property="FontWeight" Value="Bold"/>
         <Setter Property="FontSize" Value="12"/>
     </Style>                                                        </DataGridTemplateColumn.HeaderStyle>                                                        <DataGridTemplateColumn.CellTemplate>
     <DataTemplate>
        <TextBlock Text="{Binding ID}" TextAlignment="Center" />
     </DataTemplate>                                                       </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
aai2on
  • 13
  • 4