列标题居中和单元格内容居中
<!--DataGrid 列标题居中方法-->
<Style x:Key="ColumnHeaderStyle" TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Background" Value="Gray"/>
<Setter Property="Foreground" Value="White"/>
</Style>
<!--DataGrid单元格内容居中-->
<Style x:Key="DataGridCellStyle" TargetType="DataGridCell">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<colorConverter:StringToColorConverter x:Key="DataGridColorConverter"/>
常用样式
<!-- 列头标题栏样式 -->
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Background" Value="Gray"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="Gray" />
<Setter Property="Height" Value="30"/>
<Setter Property="FontSize" Value="12"/>
</Style>
<!-- 单元格样式 -->
<Style TargetType="DataGridCell">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="BorderBrush" Value="Gray" />
<Setter Property="FontSize" Value="12"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}" >
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="TextBlock" x:Key="centerAlignmentStyle">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="TextWrapping" Value="Wrap"/>
</Style>
<!--背景色改变必须先设置cellStyle 因为cellStyle会覆盖rowStyle样式,换行换色-->
<Style TargetType="DataGridRow">
<Setter Property="Height" Value="30"/>
<Style.Triggers>
<Trigger Property="AlternationIndex" Value="0">
<!--单元格背景色-->
<Setter Property="Background" Value="White" />
<!--ffe14d-->
</Trigger>
<Trigger Property="AlternationIndex" Value="1">
<!--<Setter Property="Background" Value="#f2f2f2" />-->
<Setter Property="Background" Value="Blue" />
<!--f1ef9f-->
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#f1ef9f" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#05c4ff"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="#05c4ff"/>
</Trigger>
</Style.Triggers>
</Style>
<!--网格线颜色-->
<Style TargetType="DataGrid">
<!--该属性指示是否允许用户调整列宽度-->
<Setter Property="CanUserResizeColumns" Value="false" />
<!--<Setter Property="Background" Value="#2d323c" />-->
<Setter Property="Background" Value="White" />
<Setter Property="BorderBrush" Value="Gray" />
<Setter Property="SelectionUnit" Value="FullRow" />
<Setter Property="VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="HorizontalScrollBarVisibility" Value="Auto" />
<Setter Property="RowHeaderWidth" Value="0" />
<Setter Property="HorizontalGridLinesBrush">
<Setter.Value>
<SolidColorBrush Color="#d6c79b" />
</Setter.Value>
</Setter>
<Setter Property="VerticalGridLinesBrush">
<Setter.Value>
<SolidColorBrush Color="#d6c79b" />
</Setter.Value>
</Setter>
</Style>
每一行添加序号
1,在XAML页面中的DataGrid中添加一列
<DataGridTextColumn Header="序号" Width="*" Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridRow}}, Path=Header}" CellStyle="{StaticResource DataGridCellStyle}"/>
2,在DataGrid上添加一个LoadingRow事件,该事件的处理方法如下
private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = e.Row.GetIndex() + 1;
}