question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Group Box is not rendering correct

See original GitHub issue
  • .NET Core Version: .net 6
  • Windows version: Windows 11 Version 22H2 Build 22621.674
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: I dont know

Problem description:

Group Box is rendering wrong.

Screenshot 2022-10-15 205308

Source Code:

<Window x:Class="MyProgram.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Main Window"
        Height="720" Width="1280"
        MinHeight="720" MinWidth="1280"
        WindowStartupLocation="CenterScreen" Icon="icon.ico"
        WindowStyle="ThreeDBorderWindow" ResizeMode="CanResize">
    <Grid>
        <GroupBox BorderThickness="5" BorderBrush="Red" Margin="50"/> // BorderThickness >= 13 looks normal
    </Grid>
</Window>

Expected behavior:

Group Box is rendering correctly

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JackFeyescommented, Jan 26, 2023

Just so anyone stumbling across this topic can find a resolution, here is the way I deal with this issue: In a WPF resource (global or in <Window.Resources> add the following:

<BorderGapMaskConverter x:Key="BorderGapMaskConverter"/>
<Style x:Key="FixedGroupBox" TargetType="{x:Type GroupBox}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupBox}">
                        <Grid SnapsToDevicePixels="true">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="6"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="6"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="6"/>
                            </Grid.RowDefinitions>
                            <Border Background="{TemplateBinding Background}" BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}"
                                        CornerRadius="4" Grid.Column="0" Grid.ColumnSpan="4" Grid.RowSpan="3" Grid.Row="1"/>
                            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="4" Grid.ColumnSpan="4" Grid.RowSpan="3" Grid.Row="1" Grid.Column="0">
                                <Border.OpacityMask>
                                    <MultiBinding ConverterParameter="7" Converter="{StaticResource BorderGapMaskConverter}">
                                        <Binding ElementName="Header" Path="ActualWidth"/>
                                        <Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}"/>
                                        <Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}"/>
                                    </MultiBinding>
                                </Border.OpacityMask>
                            </Border>
                            <Border x:Name="Header" Grid.Column="1" Padding="3,1,3,0" Grid.RowSpan="2" Grid.Row="0">
                                <ContentPresenter ContentSource="Header" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Border>
                            <ContentPresenter Grid.Column="1" Grid.ColumnSpan="2" Margin="{TemplateBinding Padding}" Grid.Row="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Then when you use a GroupBox set it style like this:

<GroupBox Style="{StaticResource FixedGroupBox}" x:Name="someName" BorderThickness="2" BorderBrush="#FF5CB32D" Foreground="#FF5CB32D" Header="YOUR HEADER TEXT" >
                <content goes here/>
</GroupBox>

Hope it helps someone

1reaction
BlyZeYTcommented, Oct 29, 2022

As for the fix, I don’t see how we can do this without breaking changes. I sort of managed to compromise BorderGapMaskConverter into a more or less compatible way to correctly position the opacity mask, however, positioning the actual header where it should be is not that trivial, since the Grid’s column widths cannot be databound to accommodate for the border thickness. I think the whole GroupBox template would need to be rethought from scratch.

I got the issue fixed thanks for helping 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sub-Classed Groupbox does not properly draw the contents
This is a known issue when owner-drawing a container that stores a control collection like a GroupBox ? What I'm missing to do...
Read more >
Groupbox rendering issue
I have a weird issue with the rendering of the GroupBox, I Created a custom skin, and changed the Group panel images in...
Read more >
Question - change the style of the group box
How can I change the style of the groupbox exactly like what you see in the picture? Similar to the part I highlighted...
Read more >
GroupBoxRenderer Class (System.Windows.Forms)
Provides methods used to render a group box control with or without visual styles. This class cannot be inherited.
Read more >
Solved: Galleries not rendering properly after publishing
Solved: Hello, I am running into an issue where my galleries load just fine in the designer when I click Preview. However, when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found