Align the contents of a DataGrid row vertically

I have a regular DataGrid from WPF 4.0 RTM where I put data from a database. To make the DataGrid look clean and light, I use tall / tall rows and by default, the DataGrid aligns the contents of the row in the upper vertical position, but I want to set the vertical alignment to the center.

I already tried to use this property

 VerticalAlignment="Center" 

in the DataGrid options, but that doesn't help me.

Here is an example of a XAML code describing my DataGrid without vertical center alignment:

 <DataGrid x:Name="ContentDataGrid" Style="{StaticResource ContentDataGrid}" ItemsSource="{Binding}" RowEditEnding="ContentDataGrid_RowEditEnding"> <DataGrid.Columns> <DataGridTextColumn Header="UserID" Width="100" IsReadOnly="True" Binding="{Binding Path=userID}" /> <DataGridTextColumn Header="UserName" Width="100" Binding="{Binding Path=userName}" /> <DataGridTextColumn Header="UserAccessLevel" Width="100" Binding="{Binding Path=userAccessLevel}" /> <DataGridTextColumn Header="UserPassword" Width="*" Binding="{Binding Path=userPassword}" /> </DataGrid.Columns> </DataGrid> 

The result of executing this code:

alt text

As you can see, the entire contents of the line has top vertical alignment.

What do I need to add to get the center alignment of each row content?

+51
c # vertical-alignment wpf xaml datagrid
Oct 20 '10 at 18:50
source share
8 answers

A complete solution to this problem on MSDN: Vertical alignment of the contents of a DataGrid row

In short, in a set of file styles:

 <!--body content datagrid cell vertical centering--> <Style x:Key="Body_Content_DataGrid_Centering" 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> 

In the file window:

 <DataGrid x:Name="ContentDataGrid" Style="{StaticResource ContentDataGrid}" CellStyle="{StaticResource Body_Content_DataGrid_Centering}" ItemsSource="{Binding}" RowEditEnding="ContentDataGrid_RowEditEnding"> <DataGrid.Columns> <DataGridTextColumn Header="UserID" Width="100" IsReadOnly="True" Binding="{Binding Path=userID}" /> <DataGridTextColumn Header="UserName" Width="100" Binding="{Binding Path=userName}" /> <DataGridTextColumn Header="UserAccessLevel" Width="100" Binding="{Binding Path=userAccessLevel}" /> <DataGridTextColumn Header="UserPassword" Width="*" Binding="{Binding Path=userPassword}" /> </DataGrid.Columns> </DataGrid> 

This will give you the desired result:

alt text

+101
Oct 22 '10 at 10:12
source share

To set individual text alignments, you can use:

 <DataGridTextColumn.ElementStyle> <Style TargetType="TextBlock"> <Setter Property="TextAlignment" Value="Center" /> </Style> </DataGridTextColumn.ElementStyle> 
+48
Dec 05 '11 at 11:28
source share

The following code will vertically align the contents of a DataGridTextColumn cell:

 <DataGridTextColumn.ElementStyle> <Style TargetType="TextBlock"> <Setter Property="VerticalAlignment" Value="Center"></Setter> </Style> </DataGridTextColumn.ElementStyle> 

Edit: I went back to this problem and found that the solution below works better, it will concentrate the contents of all cells in the DataGridTextRows both horizontally and vertically.

 <UserControl.Resources> <ResourceDictionary> <Style TargetType="DataGridCell"> <Setter Property="HorizontalAlignment" Value="Stretch"></Setter> <Setter Property="VerticalAlignment" Value="Stretch"></Setter> <Setter Property="VerticalContentAlignment" Value="Stretch"></Setter> <Setter Property="TextBlock.TextAlignment" Value="Center"></Setter> <Setter Property="TextBlock.VerticalAlignment" Value="Center"></Setter> </Style> </ResourceDictionary> </UserControl.Resources> 
+19
Oct 09 '13 at 15:22
source share

You can also do without overriding the ControlTemplate:

  <Style TargetType="{x:Type DataGridCell}"> <Setter Property="VerticalAlignment" Value="Center" /> </Style> 
+12
Feb 11 '14 at 19:29
source share

It works for me

  <DataGrid.CellStyle> <Style TargetType="DataGridCell"> <Setter Property="TextBlock.TextAlignment" Value="Center"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type DataGridCell}"> <Grid Background="{TemplateBinding Background}"> <ContentPresenter VerticalAlignment="Center"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </DataGrid.CellStyle> 
+10
Aug 15 '16 at 14:57
source share

The value of the VerticalAlignment="Center" attribute will center the DataGrid in its parent element.

You probably want a VerticalContentAlignment .

+5
Oct 20 '10 at 19:20
source share

Based on Jamie's answer, the following code did the trick for me when using auto-generated columns:

 Style VerticalCenterStyle = new Style(); public MainWindow() { // This call is required by the designer. InitializeComponent(); VerticalCenterStyle.Setters.Add(new Setter(VerticalAlignmentProperty, VerticalAlignment.Center)); } private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { if (e.Column is DataGridTextColumn) { ((DataGridTextColumn)e.Column).ElementStyle = VerticalCenterStyle; } } 
+1
Mar 27 '16 at 19:29
source share

This is my simple solution and it just works great

 <DataGridTemplateColumn Header="Hello" Width="200"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="World!" TextAlignment="Center" VerticalAlignment="Center"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> 

I set the width to 200 so you can notice the difference.

0
Jul 24. '19 at 19:57
source share



All Articles