WPF設定DataGrid列的標題格式 阿新 • • 發佈:2019-01-10 在Page類中設定,如果是Window,則將Page換成Window即可 <Page.Resources> <Style x:Key="CenterStyle" TargetType="{x:Type TextBlock}"> <Setter Property="HorizontalAlignment" Value="Center" /> <Setter Property="VerticalAlignment" Value="Center" /> </Style> </Page.Resources> <DataGrid x:Name ="_dataGrid" Grid.Row="1" ItemsSource="{Binding}" HorizontalAlignment="Stretch" Margin="10,10,10,20" VerticalAlignment="Stretch" CanUserSortColumns="false" HeadersVisibility="Column" AutoGenerateColumns="False" CanUserAddRows="False" HorizontalGridLinesBrush="Gray" VerticalGridLinesBrush="Gray" RowHeight="30" Background="White" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled" LoadingRow="gridFlag_LoadingRow"> <DataGrid.Columns> <!--Column1 單元列--> <DataGridTextColumn Header="Column1" Width="298" ElementStyle="{StaticResource CenterStyle}" Binding="{Binding Path=Column1}"> <DataGridTextColumn.HeaderStyle> <Style TargetType="{x:Type DataGridColumnHeader}"> <Style.Setters> <Setter Property="FontSize" Value="16"/> <Setter Property="HorizontalContentAlignment" Value="Center"/> </Style.Setters> </Style> </DataGridTextColumn.HeaderStyle> </DataGridTextColumn> <!--Column2 單元列--> <DataGridTextColumn Header="Column1value" Width="140" ElementStyle="{StaticResource CenterStyle}" Binding="{Binding Path=Column1}"> <DataGridTextColumn.HeaderStyle> <Style TargetType="{x:Type DataGridColumnHeader}"> <Style.Setters> <Setter Property="FontSize" Value="16"/> <Setter Property="HorizontalContentAlignment" Value="Center"/> </Style.Setters> </Style> </DataGridTextColumn.HeaderStyle> </DataGridTextColumn> </DataGrid.Columns> <DataGrid>