How to center the content of cells in a data grid? How to center the content of cells in a data grid? wpf wpf

How to center the content of cells in a data grid?


Final solution:

<Style x:Key="DataGridContentCellCentering" TargetType="{x:Type DataGridCell}">    <Setter Property="Template">        <Setter.Value>            <ControlTemplate TargetType="{x:Type DataGridCell}">                <Grid Background="{TemplateBinding Background}">                    <ContentPresenter VerticalAlignment="Center" />                </Grid>            </ControlTemplate>        </Setter.Value>    </Setter></Style>


The following code will center the contents of cells in DataGrid:

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


use ElementStyle

 <DataGridTextColumn ElementStyle="{StaticResource Centering}"/>  <Style x:Key="Centering" TargetType="{x:Type TextBlock}">       <Setter Property="HorizontalAlignment" Value="Center" />  </Style>