wpf 中设置DataGridTextColumn的文本对齐方式

DataGrid里面存在着像DataGridColumnHeader、DataGrid、DataGridCell等相关控件的样式设置,例如让一个DataGrid里面某一列的控件内容居中显示,
例如:

  1. DataGridColumnHeader
    View Code
    <Style x:Key="ColumnHeaderStyle" TargetType="{x:Type DataGrid:DataGridColumnHeader}">
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
    </Style>

     

普通控件设定样式使用CellStyle,或者属性即可,当设定DataGridTextColumn的文本显示时候,需要设定其ElementStyle,例如:

View Code
Style部分:
<Window.Resources>
        <ResourceDictionary>
            <Style x:Key="dgCell" TargetType="TextBlock">
                <Setter Property="TextAlignment" Value="Center"/>
            </Style>
        </ResourceDictionary>
</Window.Resources>
调用部分:
<dg:DataGrid>
                <dg:DataGrid.Columns>
                    <dg:DataGridTextColumn Width="300"  Binding="{Binding Path=Wid1}"  ElementStyle="{StaticResource dgCell}">
                    </dg:DataGridTextColumn>
                </dg:DataGrid.Columns>
</dg:DataGrid>

 

posted @ 2012-07-11 12:02  帕丁顿熊  阅读(14937)  评论(2编辑  收藏  举报