Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text alignment in a WPF DataGrid

How can I align the column data to center in a WPF DataGrid?

like image 685
Prashant Cholachagudda Avatar asked Apr 06 '09 09:04

Prashant Cholachagudda


People also ask

What is content alignment in WPF?

Content Alignment The content of content controls in WPF is dealt using various properties. These two properties are HorizontalContentAlignment and VerticalContentAlignment.

What is the difference between grid and DataGrid in WPF?

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.

How do I make a DataGrid read only?

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 .

Can I use Datagridview in WPF?

DataGrid supports all styling and templating functionality of other WPF controls. DataGrid also includes default and customizable behaviors for editing, sorting, and validation.


2 Answers

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> 
like image 89
Mohammed A. Fadil Avatar answered Sep 29 '22 20:09

Mohammed A. Fadil


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> 
like image 34
Kent Boogaart Avatar answered Sep 29 '22 22:09

Kent Boogaart