31

I set the min height of a datagrid that way:

<DataGrid MinRowHeight="40">

After feeding the datagrid with datas, the text in each cell is top and left aligned. I could not find an easy way to center that text.

Any suggestions for doing that?

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
Francois
  • 851
  • 1
  • 7
  • 10

10 Answers10

51

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>
Francois
  • 851
  • 1
  • 7
  • 10
  • 3
    This is the correct solution if your grid use columns other than DataGridTextColumn. The TextBlock style solutions will add a line bellow the text if you have templated columns on the grid. If anyone could please explain what this code is doing... – GBU Oct 27 '16 at 18:26
28

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

<Style TargetType="DataGridCell">
    <Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style>
bouvierr
  • 3,563
  • 3
  • 27
  • 32
lea
  • 289
  • 3
  • 2
  • This works great when using the built in datagrid with text columns – galford13x Dec 13 '15 at 17:34
  • Note that since you're not actually setting any `DataGridCell` specific properties, you can omit the `TargetType="DataGridCell"` attribute, and declare this as a general purpose style in your app.xaml. – BrainSlugs83 Apr 23 '20 at 09:36
9

use ElementStyle

 <DataGridTextColumn ElementStyle="{StaticResource Centering}"/>

  <Style x:Key="Centering" TargetType="{x:Type TextBlock}">
       <Setter Property="HorizontalAlignment" Value="Center" />
  </Style>
Meysam Chegini
  • 942
  • 12
  • 12
9

you can use styles. i add a sample for DataGridCheckBoxColumn, i hope it put you in the right direction.

<DataGridCheckBoxColumn Header="is active" IsReadOnly="False">
      <DataGridCheckBoxColumn.ElementStyle>
            <Style TargetType="CheckBox">
                 <Setter Property="VerticalAlignment" Value="Center"/>
                  <Setter Property="HorizontalAlignment" Value="Center"/>
             </Style>
      </DataGridCheckBoxColumn.ElementStyle>
      <DataGridCheckBoxColumn.Binding>
             <Binding Path="ISACTIVE" ValidatesOnDataErrors="True" Converter="{StaticResource MyBoolToIsActiveConverter}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"
                                 NotifyOnValidationError="True" ValidatesOnExceptions="True">
                        </Binding>
                    </DataGridCheckBoxColumn.Binding>
                </DataGridCheckBoxColumn>
blindmeis
  • 22,175
  • 7
  • 55
  • 74
6

Try setting the DataGrid's Vertical and HorizontalContentAlignment to Center

<DataGrid VerticalContentAlignment="Center" HorizontalContentAlignment="Center" />

If that doesn't work, you can use the solution in this answer. It uses a style that aligns the DataGrid cell contents

Community
  • 1
  • 1
Rachel
  • 130,264
  • 66
  • 304
  • 490
5

This style will set the VerticalAlignment to Center for all DataGridCells without needing to be applied explicitly.

<Style TargetType="DataGridCell">
    <Style.Resources>
        <Style TargetType="ContentPresenter">
            <Setter Property="VerticalAlignment" Value="Center" />
        </Style>
    </Style.Resources>
</Style>

I find this to be the most elegant solution.

The Pademelon
  • 835
  • 11
  • 29
2

Modify your style tag like this.....

 <Style x:Key="CellTextCentre" TargetType="DataGridCell">
    <Setter Property="TextBlock.TextAlignment" Value="Center"></Setter>
    <Setter Property="TextBlock.VerticalAlignment" Value="Center"></Setter> 
 </Style>
Arpit Shah
  • 71
  • 7
2

This below code display datagrid cell conent in center..

<Style x:Key="DataGridCellStyle" TargetType="DataGridCell">
            <!--DISPLAY CONTENT IN MIDDLE-->
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <Grid Background="{TemplateBinding Background}">
                            <ContentPresenter VerticalAlignment="Center" />                        
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
PPr
  • 81
  • 4
0

Here is a slightly longer version of Arpit Shah's answer.

<DataGrid AutoGenerateColumns="False" IsReadOnly="True" ItemsSource="{Binding Rows}">
    <DataGrid.Resources>
        <Style x:Key="RightAligned" TargetType="DataGridCell">
            <Setter Property="HorizontalAlignment" Value="Right" />
        </Style>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTextColumn CellStyle="{StaticResource RightAligned}" 
                            Binding="{Binding Path=Quantity, Mode=OneWay}" 
                            Header="Quantity" Width="60" />
        <DataGridTextColumn Binding="{Binding Path=Category, Mode=OneWay}" 
                            Header="Category" Width="150" />
    </DataGrid.Columns>
</DataGrid>
0

Here a better approach to center the content vertically:

<Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {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>
Coden
  • 2,579
  • 1
  • 18
  • 25