DataGrid列頭標題居中及列中文字居中
阿新 • • 發佈:2018-12-17
列頭標題居中
1 <Style x:Key="ColumnHeaderStyleC" TargetType="DataGridColumnHeader"> 2 <Setter Property="HorizontalContentAlignment" Value="Center"/> 3 <Style.Triggers> 4 <Trigger Property="IsMouseOver" Value="True"> 5 <Setter Property="ToolTip"Value="Click to sort."/> 6 </Trigger> 7 </Style.Triggers> 8 </Style>
使用
<DataGrid Name="AlarmDataDG" ColumnHeaderStyle="{DynamicResource ColumnHeaderStyleC}" BeginningEdit="AlarmDataDG_BeginningEdit" CellEditEnding="AlarmDataDG_CellEditEnding">
列中文字(DataGridTemplateColumn)
1 <DataGridTemplateColumn Header="序號" Visibility="Hidden"> 2 <DataGridTemplateColumn.CellTemplate> 3 <DataTemplate> 4 <TextBlock Text="{Binding Path=RowId}" HorizontalAlignment="Center"/> 5 </DataTemplate> 6 </DataGridTemplateColumn.CellTemplate> 7 </DataGridTemplateColumn>
DataGridTextColumn 設定ElementStyle
參考https://www.cnblogs.com/peasana/archive/2012/07/11/2586031.html