Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align DataGrid Column Header to Center

Tags:

c#

wpf

datagrid

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?

like image 631
Bishan Avatar asked Oct 02 '13 06:10

Bishan


2 Answers

Check this

<DataGridTextColumn Header="Nombre"                           Binding="{Binding Nombre}"> <DataGridTextColumn.HeaderStyle>   <Style TargetType="DataGridColumnHeader">      <Setter Property="HorizontalContentAlignment"                  Value="Center" />   </Style> </DataGridTextColumn.HeaderStyle> 
like image 99
Anjali Avatar answered Sep 24 '22 02:09

Anjali


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}"/> 
like image 21
Evgeniy Lyakh Avatar answered Sep 21 '22 02:09

Evgeniy Lyakh