数据网格文本对齐 [英] Datagrid text alignment

查看:20
本文介绍了数据网格文本对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人有一种简单的方法可以使 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 上居中,它将被 DataGridTextColumns 中使用的 TextBlocks 和 TextBoxes 继承,并使文本居中:

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" />

如果你想对齐单元格中的文本而不是标题或网格中的其他地方,你可以使用 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>

这篇关于数据网格文本对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆