Align DataGrid Column Header to Center

2020-05-20 07:13发布

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?

标签: c# wpf datagrid
6条回答
对你真心纯属浪费
2楼-- · 2020-05-20 07:42

Check this

<DataGridTextColumn Header="Nombre"
                          Binding="{Binding Nombre}">
<DataGridTextColumn.HeaderStyle>
  <Style TargetType="DataGridColumnHeader">
     <Setter Property="HorizontalContentAlignment"
                 Value="Center" />
  </Style>
</DataGridTextColumn.HeaderStyle>
查看更多
smile是对你的礼貌
3楼-- · 2020-05-20 07:42

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}"/>
查看更多
Root(大扎)
4楼-- · 2020-05-20 07:54

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));
    }

}
查看更多
够拽才男人
5楼-- · 2020-05-20 07:54

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>
查看更多
女痞
6楼-- · 2020-05-20 08:00

Try this

<DataGridTextColumn.CellStyle>
  <Style>
    <Setter Property="HorizontalAlignment" Value="Center" />
  </Style>
</DataGridTextColumn.CellStyle>
查看更多
做个烂人
7楼-- · 2020-05-20 08:06

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>
查看更多
登录 后发表回答