WPF Groupbox Header Position Alignment

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

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

But I want to set its position relative to the GroupBox contour.

+7
alignment header wpf groupbox
source share
2 answers

It's simple! Just edit the GroupBox Template:

In Blend, do the following:

  • Right-click GroupBox> Modify Template> Modify Copy> OK
  • Search for the next 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

  • Also set HorizontalAlignment="Right"

You just aligned the title on the right! But a white gap ran past him. For this

  • Now find the 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

  • Just above, add the following code (this will shift the β€œwhite gap” behind the header to the right:

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

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

Please tell me if this is what you need.

+12
source share

Changing the header change of the group header will result in non-OS control.

Therefore, I think that you cannot change this using the default styles. Custom template will solve your problem.

+2
source share

All Articles