如何设置WPF中DataGrid头的样式 [英] How to Set style for DataGrid header in WPF
本文介绍了如何设置WPF中DataGrid头的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 DataGrid
:
< DataGrid AutoGenerateColumns =FalseHeight =221HorizontalAlignment =CenterVerticalContentAlignment =CenterHorizontalContentAlignment =Center
Margin =6,269,0,0Name =dataGrid1VerticalAlignment = 875SelectionChanged =dataGrid1_SelectionChanged>
< DataGrid.Columns>
< DataGridTextColumn x:Name =IdHeader =IDBinding ={Binding Path = Key}HeaderStyle =>
< / DataGridTextColumn>
< DataGridTemplateColumn Header =ImageWidth =SizeToCellsIsReadOnly =TrueMinWidth =80>
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< Image Width =16Height =16Source ={StaticResource MyImageSource}HorizontalAlignment =CenterVerticalAlignment =Center/>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>
< /DataGrid.Columns>
< / DataGrid>
如何集中 DataGrid Header
?并应用它的样式?
感谢
):
< DataGrid。 ColumnHeaderStyle>
< Style TargetType ={x:Type DataGridColumnHeader}>
< Setter Property =FontWeightValue =Bold/>
< Setter Property =HorizontalAlignmentValue =Center/>
< Setter Property =HorizontalContentAlignmentValue =Center/>
< / Style>
< /DataGrid.ColumnHeaderStyle>
网格列成为:
为什么?
$为了避免标题合拢的影响,只需按照这种方式修正你的风格:<$> p $ p>
< Setter Property =HorizontalAlignmentValue =Stretch/>
< Setter Property =HorizontalContentAlignmentValue =Center/>
I have a DataGrid
like this:
<DataGrid AutoGenerateColumns="False" Height="221" HorizontalAlignment="Center" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"
Margin="6,269,0,0" Name="dataGrid1" VerticalAlignment="Center" Width="875" SelectionChanged="dataGrid1_SelectionChanged">
<DataGrid.Columns >
<DataGridTextColumn x:Name="Id" Header="ID" Binding="{Binding Path=Key}" HeaderStyle="">
</DataGridTextColumn>
<DataGridTemplateColumn Header="Image" Width="SizeToCells" IsReadOnly="True" MinWidth="80">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Width="16" Height="16" Source="{StaticResource MyImageSource}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
How I can center DataGrid Header
? and apply style for it?
thanks
Edit 1):
after write this code:
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</DataGrid.ColumnHeaderStyle>
grid columns become :
Why?
解决方案
To avoid the effect of the header collapsing simply correct your style in this way:
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
这篇关于如何设置WPF中DataGrid头的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文