简体   繁体   English

WPF - DataGrid列标题对齐

[英]WPF - DataGrid Column Header Alignment

I'm using the WPFToolkit DataGrid control and I wanted to restyle some of the column headers so that the header text is displayed vertically instead of horizontally (the data in the column is all numeric and therefore, not very wide, but the header text is long). 我正在使用WPFToolkit DataGrid控件,我想重新设置一些列标题,以便标题文本垂直显示而不是水平显示(列中的数据都是数字,因此,不是很宽,但标题文本是长)。 So I created a DataTemplate to and tried to get the DataGridColumn.HeaderTemplate to it. 所以我创建了一个DataTemplate并尝试将DataGridColumn.HeaderTemplate添加到它。 This is my template: 这是我的模板:

<DataTemplate x:Key="headerTemplate"> 
        <StackPanel VerticalAlignment="Bottom" HorizontalAlignment="Left" Background="Aqua"> 
            <StackPanel.LayoutTransform> 
                <RotateTransform Angle="-90"/> 
            </StackPanel.LayoutTransform> 
            <TextBlock Text="{Binding}" VerticalAlignment="Bottom" HorizontalAlignment="Left" Background="Pink"> 
            </TextBlock> 
        </StackPanel> 
    </DataTemplate>

This works just fine, except the alignment of the header is always left and center. 这很好用,除了标题的对齐总是左和中。 No combination of alignments for the StackPanel or the TextBlock seems to make any difference. StackPanel或TextBlock的对齐组合似乎没有任何区别。 I would like to have the text aligned at the bottom and middle. 我希望文本在底部和中间对齐。 How can I make it do that? 我该怎么做呢?

Thanks, 谢谢,

AT

OK, found the answer. 好的,找到了答案。

The property I was looking for was VerticalContentAlignment . 我正在寻找的属性是VerticalContentAlignment

I created a style and attached that using the HeaderStyle property, and it worked :) 我创建了一个样式,并使用HeaderStyle属性附加,它工作:)

<Style x:Key="VerticalGridHeaderStyle" TargetType="tk:DataGridColumnHeader">
    <Setter Property="VerticalContentAlignment" Value="Bottom"/>
</Style>

This also works 这也有效

<Style TargetType="{x:Type DataGridColumnHeader}">
   <Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>

Add the x:Key="" if you don't want to target all DataGridColumnHeader's 如果您不想定位所有DataGridColumnHeader,请添加x:Key=""

If you don't want to mess up the style that you have already applied, use BasedOn: 如果您不想搞乱已经应用的样式,请使用BasedOn:

<DataGrid.ColumnHeaderStyle>
                <Style BasedOn="{StaticResource MetroDataGridColumnHeader}" TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGrid.ColumnHeaderStyle>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM