WPF - DataGrid 列标题对齐

发布于 2024-08-25 10:53:38 字数 820 浏览 5 评论 0原文

我正在使用 WPFToolkit DataGrid 控件,我想重新设置一些列标题的样式,以便标题文本垂直显示而不是水平显示(列中的数据都是数字,因此不是很宽,但标题文本是长的)。 因此,我创建了一个 DataTemplate 并尝试将 DataGridColumn.HeaderTemplate 获取到它。这是我的模板:

<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>

这工作得很好,除了标题的对齐方式始终是左对齐和居中。 StackPanel 或 TextBlock 的对齐组合似乎没有任何区别。我想让文本在底部和中间对齐。我怎样才能让它做到这一点?

谢谢,

AT

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).
So I created a DataTemplate to and tried to get the DataGridColumn.HeaderTemplate to it. 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. I would like to have the text aligned at the bottom and middle. How can I make it do that?

Thanks,

AT

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇俏 2024-09-01 10:53:38

好的,找到答案了。

我正在寻找的属性是VerticalContentAlignment

我创建了一个样式并使用 HeaderStyle 属性附加它,它起作用了:)

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

OK, found the answer.

The property I was looking for was VerticalContentAlignment.

I created a style and attached that using the HeaderStyle property, and it worked :)

<Style x:Key="VerticalGridHeaderStyle" TargetType="tk:DataGridColumnHeader">
    <Setter Property="VerticalContentAlignment" Value="Bottom"/>
</Style>
瑾兮 2024-09-01 10:53:38

如果您不想弄乱已经应用的样式,请使用 BasedOn:

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

If you don't want to mess up the style that you have already applied, use BasedOn:

<DataGrid.ColumnHeaderStyle>
                <Style BasedOn="{StaticResource MetroDataGridColumnHeader}" TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGrid.ColumnHeaderStyle>
天煞孤星 2024-09-01 10:53:38

这也可以添加 x:Key=""

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

如果您不想定位所有 DataGridColumnHeader,

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文