I was wondering if anyone had an easy way to get the text in a WPF data grid to be center aligned. I got the data grid to work just fine, but the right text alignment bothered me. I goggled some, and downloaded the wpftoolkit, but the examples either do not work, or give me a compile error. I did add the reference to the wpftoolkit to my project. Any help would be appreciated. Thank you
the xaml for the data grid is as follows
<WpfToolkit:DataGrid AutoGenerateColumns="True" Margin="15,15,10,65" Name="DG1" CanUserReorderColumns="False" />
If you set the Block.TextAlignment property to Center on the DataGrid, it will be inherited by the TextBlocks and TextBoxes used in DataGridTextColumns and will center the text:
<WpfToolkit:DataGrid
Block.TextAlignment="Center"
AutoGenerateColumns="True"
Margin="15,15,10,65"
Name="DG1"
CanUserReorderColumns="False" />
If you want to align text in the cells but not in the headers or elsewhere in the Grid, you can set the property on the DataGridCell using CellStyle:
<WpfToolkit:DataGrid
AutoGenerateColumns="True"
Margin="15,15,10,65"
Name="DG1"
CanUserReorderColumns="False">
<WpfToolkit:DataGrid.CellStyle>
<Style TargetType="WpfToolkit:DataGridCell">
<Setter Property="Block.TextAlignment" Value="Center"/>
</Style>
</WpfToolkit:DataGrid.CellStyle>
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