Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center DataGridTemplateColumn.HeaderTemplate

Tags:

wpf

xaml

datagrid

Any clue? The code below doesn't work properly...

Thank you!

 <DataGrid AutoGenerateColumns="False"  VerticalAlignment="Stretch" HorizontalAlignment="Stretch"  Name="dg1"   Grid.Row="0" >
     <DataGridTemplateColumn  Width="SizeToCells" IsReadOnly="True">
         <DataGridTemplateColumn.HeaderTemplate>
             <DataTemplate>
                 <Label HorizontalAlignment="Center" Content="First Name"></Label>
             </DataTemplate>
          </DataGridTemplateColumn.HeaderTemplate>
          <DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                 <Label Content="{Binding FirstName}" />
              </DataTemplate>
          </DataGridTemplateColumn.CellTemplate>
     </DataGridTemplateColumn>

UPDATE:

Solution of @ArsenMkrt is great but I am facing some strange vertical lines...

enter image description here

like image 685
Friend Avatar asked Dec 06 '12 16:12

Friend


2 Answers

Use HeaderStyle instead of HeaderTemplate

    <DataGridTemplateColumn.HeaderStyle>
        <Style TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="HorizontalAlignment" Value="Center"/>
        </Style>
    </DataGridTemplateColumn.HeaderStyle> 
like image 191
Arsen Mkrtchyan Avatar answered Sep 23 '22 10:09

Arsen Mkrtchyan


try this It works for me to center the header without any issue.

<DataGridTemplateColumn.HeaderStyle>
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
    </Style>
</DataGridTemplateColumn.HeaderStyle>
like image 39
Avneesh Srivastava Avatar answered Sep 22 '22 10:09

Avneesh Srivastava