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?
Check this
<DataGridTextColumn Header="Nombre" Binding="{Binding Nombre}"> <DataGridTextColumn.HeaderStyle> <Style TargetType="DataGridColumnHeader"> <Setter Property="HorizontalContentAlignment" Value="Center" /> </Style> </DataGridTextColumn.HeaderStyle>
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}"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With