Text alignment in a WPF DataGrid


Question

How can I align the column data to center in a WPF DataGrid?

1
51
8/26/2014 2:06:29 PM

Accepted Answer

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>
47
9/27/2011 3:04:22 PM

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>

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon