Text Alignment in WPF DataGrid

How to map column data to center in WPF DataGrid ?

+49
c # wpf xaml wpftoolkit
Apr 6 '09 at 9:13
source share
11 answers

It's hard to say without knowing the 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> 
+44
Apr 6 '09 at 9:45
source share
— -

If you are using a DataGridTextColumn, you can use the following code snippet:

 <Style TargetType="DataGridCell"> <Style.Setters> <Setter Property="TextBlock.TextAlignment" Value="Center" /> </Style.Setters> </Style> 
+87
Apr 28 2018-10-12T00:
source share

I started with a huttelihut solution. Unfortunately, so far this has not worked. I changed his answer and came up with this (the solution is to align the text to the right):

 <Resources> <Style x:Key="RightAligned" TargetType="TextBlock"> <Setter Property="HorizontalAlignment" Value="Right"/> </Style> </Resources> 

As you can see, I applied the style to a TextBlock, not a DataGridCell.

And then I had to set the Element style, not the Cell style.

 ElementStyle="{StaticResource RightAligned}" 
+19
09 May '13 at 10:17
source share

+1 for Kent Boogart. I ended up doing this, making the code a bit less cluttered (and allowing me to use multi-column alignment):

 <Resources> <Style x:Key="NameCellStyle" TargetType="DataGridCell"> <Setter Property="HorizontalAlignment" Value="Center" /> </Style> </Resources> <DataGrid.Columns> <DataGridTextColumn Header="Name" CellStyle="{StaticResource NameCellStyle}" Binding="{Binding Name}"/> // .. other columns </DataGrid.Columns> 
+14
Mar 28 2018-11-11T00: 00Z
source share

Here's @MohammedAFadil's XAML response converted to C # code behind:

 var MyStyle = new Style(typeof(DataGridCell)) { Setters = { new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center) } }; 

To apply Style , set the CellStyle property for the DataGrid , for example

 var MyGrid = new DataGrid() { CellStyle = MyStyle }; 
+9
Aug 26 '14 at 2:04 on
source share

Or in the code behind:

 grid.CellStyle = newCellStyle(); public static Style newCellStyle() { //And here is the C# code to achieve the above System.Windows.Style style = new Style(typeof(DataGridCell)); style.Setters.Add(new System.Windows.Setter { Property = Control.HorizontalAlignmentProperty, Value = HorizontalAlignment.Center }); return style; } 
+4
Aug 23 2018-12-12T00:
source share

I'm having trouble moving the cell and funky using the accepted answer. I know this late, but hopefully my results will help someone. I use:

 <DataGridTextColumn.ElementStyle> <Style> <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/> </Style> 

not CellStyle.

+3
Jul 03 '13 at 15:12
source share

Ok, I used the frameworkElement approach, but there was a weird behavior when you try to highlight a row.

I added another example of WPat Datagrid alignment in this thread !

+1
Aug 24 '09 at 19:50
source share

My favorite solution:

 <DataGridTextColumn Header="My Column" Binding="{Binding MyDBValue}" Width="100" > <DataGridTextColumn.CellStyle> <Style> <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/> </Style> </DataGridTextColumn.CellStyle> 

+1
Jul 09 2018-11-11T00:
source share

Thanks to Danny Beckett for translating @MohammedAFadil's XAML response converted to C # code. All my datagrids are dynamically configured, so I can change something whenever I want.

To set an empty datagrid, there is nothing in it, and then just bind it to the data, just take your datagrid.columns

  var centerTextSetter = new Style(typeof(DataGridCell)) { Setters = { new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center) } }; DgDbNames.Columns.Add(new DataGridTextColumn() { Header = "Db Name", Binding = new System.Windows.Data.Binding("DbName"), IsReadOnly = true, Width = new DataGridLength(0.2, DataGridLengthUnitType.Star), CellStyle = centerTextSetter }); 
0
Oct 18 '17 at 20:07 on
source share

It works well for me

 <DataGridTextColumn Width="1*" Binding="{Binding Balance, StringFormat=C} "Header="Balance"> <DataGridTextColumn.CellStyle> <Style> <Setter Property="TextBlock.TextAlignment" Value="Right"/> </Style> </DataGridTextColumn.CellStyle> </DataGridTextColumn> 
0
Apr 02 '19 at 20:19
source share



All Articles