DataGrid中的文本对齐 [英] Text alignment in DataGrid

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

问题描述

我正在编程WPF。我需要一种在DataGrid控件中创建单元格的中心内容的方法。我也使用这个代码:

 < DataGrid x:Name =dg1Horizo​​ntalAlignment =LeftMargin =10, 10,0,0
VerticalAlignment =TopHeight =360Width =498
FontFamily =2 BadrFontSize =18
AlternatingRowBackground =LightCoralFlowDirection =RightToLeft
Horizo​​ntalContentAlignment =CenterVerticalContentAlignment =Center
Background ={x:Null}/>

有什么问题?



请问您在XAML(只是XAML )中提供您的答案。




解决方案

你需要设置DataGridCell样式

 < DataGrid> 
< DataGrid.Columns>
< DataGridTextColumn>
< DataGridTextColumn.ElementStyle>
< Style TargetType =TextBlock>
< Setter Property =Horizo​​ntalAlignmentValue =Center/>
< / Style>
< /DataGridTextColumn.ElementStyle>
< / DataGridTextColumn>
< /DataGrid.Columns>
< / DataGrid>


I'm programming by WPF. I need a way to make center content of cells, in DataGrid control. I use this code also:

<DataGrid x:Name="dg1" HorizontalAlignment="Left" Margin="10,10,0,0"
    VerticalAlignment="Top" Height="360" Width="498"
    FontFamily="2  Badr" FontSize="18" 
    AlternatingRowBackground="LightCoral" FlowDirection="RightToLeft"
    HorizontalContentAlignment="Center" VerticalContentAlignment="Center" 
    Background="{x:Null}"/>

What is wrong?

What can I do?

Please offer me your answers in XAML (Just XAML).

解决方案

You need set DataGridCell style

    <DataGrid>
        <DataGrid.Columns>
            <DataGridTextColumn>
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="TextBlock">
                        <Setter Property="HorizontalAlignment" Value="Center" />
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

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

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