wpf 如何将 DataGridTemplateColumn.HeaderTemplate 居中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13748024/
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-13 06:31:28  来源:igfitidea点击:

How to center DataGridTemplateColumn.HeaderTemplate

wpfxamldatagrid

提问by Developer

Any clue? The code below doesn't work properly...

有什么线索吗?下面的代码不能正常工作...

Thank you!

谢谢!

 <DataGrid AutoGenerateColumns="False"  VerticalAlignment="Stretch" HorizontalAlignment="Stretch"  Name="dg1"   Grid.Row="0" >
     <DataGridTemplateColumn  Width="SizeToCells" IsReadOnly="True">
         <DataGridTemplateColumn.HeaderTemplate>
             <DataTemplate>
                 <Label HorizontalAlignment="Center" Content="First Name"></Label>
             </DataTemplate>
          </DataGridTemplateColumn.HeaderTemplate>
          <DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                 <Label Content="{Binding FirstName}" />
              </DataTemplate>
          </DataGridTemplateColumn.CellTemplate>
     </DataGridTemplateColumn>

UPDATE:

更新:

Solution of @ArsenMkrt is great but I am facing some strange vertical lines...

@ArsenMkrt 的解决方案很棒,但我面临一些奇怪的垂直线......

enter image description here

在此处输入图片说明

回答by Arsen Mkrtchyan

Use HeaderStyleinstead of HeaderTemplate

使用HeaderStyle代替HeaderTemplate

    <DataGridTemplateColumn.HeaderStyle>
        <Style TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="HorizontalAlignment" Value="Center"/>
        </Style>
    </DataGridTemplateColumn.HeaderStyle> 

回答by Avneesh Srivastava

try this It works for me to center the header without any issue.

试试这个它对我有用,可以毫无问题地将标题居中。

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

回答by Supercriss

not sure if still helps but with this:

不知道是否仍然有帮助,但与此:

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

will center the header without the vertical bar issue

将标题居中而不会出现竖条问题

回答by Anup Sharma

All the answers here did solve the problem, but the theme was different from rest of the column headers. So A little change did the trick for me. Here it is if anyone is still looking for it.

这里的所有答案都解决了问题,但主题与列标题的其余部分不同。所以一点点改变对我有用。如果有人还在寻找它,这里就是它。

<DataGridTemplateColumn.HeaderStyle>
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="Background" Value="Transparent"/>
    </Style>
</DataGridTemplateColumn.HeaderStyle>