wpf 如何在 xaml 的 DataGrid 列标题中右对齐文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8810507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:14:06  来源:igfitidea点击:

How to right align text in a DataGrid column header in xaml?

wpfxamldatagridheaderalignment

提问by user763554

I have a WPF DataGrid with a column header as follows:

我有一个带有列标题的 WPF DataGrid,如下所示:

<DataGridTemplateColumn Header="Length" Width="100">
     ...
</DataGridTemplateColumn>

How do I make this header align right? Thanks. I know how to align the column content. Emphasis is aligning COLUMN HEADER.

如何使此标题正确对齐?谢谢。我知道如何对齐列内容。重点是对齐 COLUMN HEADER。

回答by H.B.

Set the HorizontalContentAlignmentof the header using the HeaderStyle:

使用以下命令设置HorizontalContentAlignment标题的HeaderStyle

<DataGridTemplateColumn.HeaderStyle>
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="HorizontalContentAlignment" Value="Right"/>
    </Style>
</DataGridTemplateColumn.HeaderStyle>

回答by synergetic

H.B's answer is correct; just add one more line:

HB的回答是正确的;只需再添加一行:

<DataGridTextColumn.HeaderStyle>
    <Style TargetType="{x:Type DataGridColumnHeader}">
      <Setter Property="HorizontalAlignment" Value="Stretch"/>
      <Setter Property="HorizontalContentAlignment" Value="Right"/>
    </Style>
</DataGridTextColumn.HeaderStyle>