Datagrid文本对齐 [英] Datagrid text alignment

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

问题描述

我想知道,如果有人有一个简单的方法来获取文本在WPF数据网格中心对齐。我得到的数据网格工作很好,但正确的文本对齐困扰我。我goggled一些,并下载了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的中心,它将被TextBlocks和TextBox继承用于DataGridTextColumns并将以文本为中心:

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>

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

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