WPF 中设置DataGridTextColumn的文本对齐方式
DataGrid里面存在着像DataGridColumnHeader、DataGrid、DataGridCell等相关控件的样式设置,例如让一个DataGrid里面某一列的控件内容居中显示
普通控件设定样式使用CellStyle,或者属性即可,当设定DataGridTextColumn的文本显示时候,需要设定其ElementStyle
DataGrid里面存在着像DataGridColumnHeader、DataGrid、DataGridCell等相关控件的样式设置,例如让一个DataGrid里面某一列的控件内容居中显示
<DataGrid Name="datagrid" Grid.Row="2" CanUserResizeRows="False" RowHeaderWidth="0" HorizontalGridLinesBrush="#999" VerticalGridLinesBrush="#999" AutoGenerateColumns="False" BorderBrush="#999" BorderThickness="1" IsReadOnly="True" Margin="0,5,0,0" SelectionChanged="datagrid_SelectionChanged">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
<Setter Property="VerticalContentAlignment" Value="Center"></Setter>
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="BorderThickness" Value="0"></Setter>
</Style>
</DataGrid.CellStyle>
</DataGrid
<Style x:Key="ColumnHeaderStyle" TargetType="{x:Type DataGrid:DataGridColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
普通控件设定样式使用CellStyle,或者属性即可,当设定DataGridTextColumn的文本显示时候,需要设定其ElementStyle
<UserControl.Resources>
<Style x:Key="CenterAlignCell" TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Center"/>
</Style>
<Style x:Key="RightAlignCell" TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Right"/>
</Style>
</UserControl.Resources>