40

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?

2

6 Answers 6

83

Check this

<DataGridTextColumn Header="Nombre"
                          Binding="{Binding Nombre}">
<DataGridTextColumn.HeaderStyle>
  <Style TargetType="DataGridColumnHeader">
     <Setter Property="HorizontalContentAlignment"
                 Value="Center" />
  </Style>
</DataGridTextColumn.HeaderStyle>
2
  • 3
    Would be appreciated any comment why Your solution does work and not solution of @Bishan. Dec 12, 2016 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.... Jun 5, 2019 at 20:14
19

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}"/>
1
  • 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. Sep 30, 2020 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));
    }

}
0

Try this

<DataGridTextColumn.CellStyle>
  <Style>
    <Setter Property="HorizontalAlignment" Value="Center" />
  </Style>
</DataGridTextColumn.CellStyle>
0
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>
3
  • The question is about changing a single column. Nov 19, 2019 at 18:51
  • @ChrisBordeman: Styles can be keyed to apply them to a specific scope.
    – dotNET
    Nov 20, 2019 at 4:46
  • Of course, but then you should show that instead of this. Nov 20, 2019 at 18:33
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>

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.