Text alignment in a WPF DataGrid Text alignment in a WPF DataGrid wpf wpf

Text alignment in a WPF DataGrid


If you are using DataGridTextColumn you can use the following code snippet:

<Style TargetType="DataGridCell">     <Style.Setters>            <Setter Property="TextBlock.TextAlignment" Value="Center" />     </Style.Setters></Style>


It's hard to say without knowing specifics, but here's a DataGridTextColumn that is centered:

<wpf:DataGridTextColumn Header="Name" Binding="{Binding Name}" IsReadOnly="True">    <wpf:DataGridTextColumn.CellStyle>        <Style>            <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>        </Style>    </wpf:DataGridTextColumn.CellStyle></wpf:DataGridTextColumn>


I started with huttelihut's solution. Unfortunately, that didn't work for me just yet. I tweaked his answer and came up with this (solution is to align the text to the right):

<Resources>    <Style x:Key="RightAligned" TargetType="TextBlock">        <Setter Property="HorizontalAlignment" Value="Right"/>    </Style></Resources>

As you can see, I applied the style to a TextBlock, not the DataGridCell.

And then I had to set the Element style, not the Cell style.

ElementStyle="{StaticResource RightAligned}"