简体   繁体   English

WPF Groupbox标头位置对齐

[英]WPF Groupbox header position alignment

Is it possible to set the position alignment for a GroupBox Header in WPF? 是否可以在WPF中设置GroupBox Header的位置对齐? 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. 默认设置是放在GroupBox轮廓的左上角,但我希望它在顶部居中。 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>

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: 只需编辑GroupBox模板:

In Blend, do the following : 在Blend中,执行以下操作:

  • Right click GroupBox > Edit Template > Edit a Copy > OK 右键单击GroupBox>编辑模板>编辑副本>确定
  • Search for following section: 搜索以下部分:

     <Border x:Name="Header" Padding="3,1,3,0" Grid.Column="1" Grid.Row="0" Grid.RowSpan="2"> ... </Border> 
  • Change Grid.Column to 2 Grid.Column更改为2

  • Also set HorizontalAlignment="Right" 同时设置HorizontalAlignment="Right"

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> 
  • Add RenderTransformOrigin="0.5,0.5" to the 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> 

You are done! 你完成了! You just got a GroupBox with right aligned header!!! 你刚刚得到一个具有右对齐标题的GroupBox!

Please tell me if this is what you required. 请告诉我这是否是您所需要的。

Changing the group-box-header alinment will result in non-OS-conform controls. 更改group-box-header alinment将导致不符合操作系统的控件。

Therefore, I think you won't be able to change this using the default styles. 因此,我认为您无法使用默认样式更改此设置。 A custom template will solve your problem. 自定义模板将解决您的问题。

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

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