本文介绍了WPF Groupbox标头位置对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在WPF中为 GroupBox Header 设置位置对齐?默认值是放置在 GroupBox 轮廓的左上角,但我希望它位于顶部的中心。我知道您可以使用以下命令设置文本的属性:

Is it possible to set the position alignment for a GroupBox Header in WPF? The default is to place in the top left corner of the GroupBox outline but I would like it to be centered at the top. I know that you can set the properties of the text using:

<GroupBox Grid.Row="1" HorizontalAlignment="Center">
   <GroupBox.Header>
      <TextBlock Text="Cash Match" Foreground="Black" FontWeight="Bold"/>
   </GroupBox.Header>
</GroupBox>

但是我想相对于设置位置GroupBox 大纲。

But I'm looking to set the position of it with respect to the GroupBox outline.

推荐答案

简单!只需编辑GroupBox模板:

It's simple! Just edit Template of GroupBox:

在Blend中,执行以下操作:

In Blend, do the following :


  • 右键单击 GroupBox>编辑模板>编辑副本>确定

  • 搜索以下部分:

  • Right click GroupBox > Edit Template > Edit a Copy > OK
  • Search for following section:

<Border x:Name="Header" Padding="3,1,3,0"
        Grid.Column="1" Grid.Row="0" Grid.RowSpan="2">
    ...
</Border>


  • Grid.Column 更改为 2

    您刚刚将标头向右对齐!!!但是,僵尸背后的空白。为此,

    You have just aligned the header to right!!! But bot the white gap behind it. For that,


    • 现在搜索以下部分:

    • Now search for following section :

    <Border BorderBrush="White" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="4" Grid.ColumnSpan="4" Grid.Row="1" Grid.RowSpan="3">
        <Border.OpacityMask>
            ...
        </Border.OpacityMask>
        ...
    </Border>
    


  • 添加 RenderTransformOrigin = 0.5,0.5 到边框

    就在上方,添加以下代码(这会将标头后面的白色间隙 移至右侧:

    Just above , add following code (this will shift the "white gap" behind header to right:

    <Border.RenderTransform>
        <ScaleTransform ScaleX="-1"/>
    </Border.RenderTransform>
    


  • 您完成!您只是有一个带有正确对齐的标题的GroupBox !!!

    You are done! You just got a GroupBox with right aligned header!!!

    请告诉我这是否是您所需要的。

    Please tell me if this is what you required.

    这篇关于WPF Groupbox标头位置对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    09-15 02:43