Search code examples
wpfdatagridrowalignment

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


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?


Solution

  • 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>