How can I align the column data to center in a WPF DataGrid
?
Content Alignment The content of content controls in WPF is dealt using various properties. These two properties are HorizontalContentAlignment and VerticalContentAlignment.
A Grid is a control for laying out other controls on the form (or page). A DataGrid is a control for displaying tabular data as read from a database for example.
To make individual columns or cells read-only, set the DataGridColumn. IsReadOnly or DataGridCell. IsReadOnly properties. If a conflict exists between the settings at the DataGrid, column, or cell levels, a value of true takes precedence over a value of false .
DataGrid supports all styling and templating functionality of other WPF controls. DataGrid also includes default and customizable behaviors for editing, sorting, and validation.
If you are using DataGridTextColumn you can use the following code snippet:
<Style TargetType="DataGridCell"> <Style.Setters> <Setter Property="TextBlock.TextAlignment" Value="Center" /> </Style.Setters> </Style>
It's hard to say without knowing 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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With