How to right align text in a DataGrid column header in xaml?

20,732

Solution 1

Set the HorizontalContentAlignment of the header using the HeaderStyle:

<DataGridTemplateColumn.HeaderStyle>
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="HorizontalContentAlignment" Value="Right"/>
    </Style>
</DataGridTemplateColumn.HeaderStyle>

Solution 2

H.B's answer is correct; just add one more line:

<DataGridTextColumn.HeaderStyle>
    <Style TargetType="{x:Type DataGridColumnHeader}">
      <Setter Property="HorizontalAlignment" Value="Stretch"/>
      <Setter Property="HorizontalContentAlignment" Value="Right"/>
    </Style>
</DataGridTextColumn.HeaderStyle>
Share:
20,732
user763554
Author by

user763554

Updated on April 22, 2020

Comments

  • user763554
    user763554 about 4 years

    I have a WPF DataGrid with a column header as follows:

    <DataGridTemplateColumn Header="Length" Width="100">
         ...
    </DataGridTemplateColumn>
    

    How do I make this header align right? Thanks. I know how to align the column content. Emphasis is aligning COLUMN HEADER.

  • dotNET
    dotNET almost 11 years
    Can you explain this a bit plz?
  • wondra
    wondra over 4 years
    It is worth pointing out this will not produce "right align text" as per question title, but rather right aligned TextBlock (with left aligned text).