Datagrid文本对齐 [英] Datagrid text alignment
问题描述
我想知道是否有人有一个简单的方法来获取WPF数据网格中的文本为中心对齐。我的数据网格工作正常,但正确的文字对齐困扰着我。我咯咯笑了一些,并下载了wpftoolkit,但是这些例子都不起作用,或给我一个编译错误。我将wpftoolkit的引用添加到我的项目中。任何帮助将不胜感激。谢谢
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
数据网格的xaml如下
the xaml for the data grid is as follows
<WpfToolkit:DataGrid AutoGenerateColumns="True" Margin="15,15,10,65" Name="DG1" CanUserReorderColumns="False" />
推荐答案
如果您设置 Block.TextAlignment 属性到DataGrid中心,它将被继承通过TextGridTextColumns中使用的TextBlocks和TextBox,并将文本居中:
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" />
如果要在单元格中对齐文本,但不在标题或Grid中的其他位置可以使用CellStyle在DataGridCell上设置属性:
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>
这篇关于Datagrid文本对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!