<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:Controls="clr-namespace:MahApps.Metro.Controls"
                    xmlns:Converters="clr-namespace:MahApps.Metro.Converters">

    <Converters:ThicknessBindingConverter x:Key="ThicknessBindingConverter" />

    <Thickness x:Key="GroupBoxHeaderThemePadding">4</Thickness>

    <Style x:Key="MetroGroupBox" TargetType="{x:Type GroupBox}">
        <Setter Property="Background" Value="{DynamicResource AccentColorBrush}" />
        <Setter Property="BorderBrush" Value="{DynamicResource AccentColorBrush}" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="Controls:ControlsHelper.ContentCharacterCasing" Value="Upper" />
        <Setter Property="Controls:ControlsHelper.HeaderFontSize" Value="{DynamicResource ContentFontSize}" />
        <Setter Property="Controls:GroupBoxHelper.HeaderForeground" Value="{x:Null}" />
        <Setter Property="Foreground" Value="{DynamicResource BlackBrush}" />
        <Setter Property="Padding" Value="4" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupBox}">
                    <Grid x:Name="GroupBoxRoot">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Border x:Name="HeaderSite"
                                Grid.Row="0"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                UseLayoutRounding="True">
                            <Controls:ContentControlEx x:Name="HeaderContent"
                                                       Margin="{DynamicResource GroupBoxHeaderThemePadding}"
                                                       Content="{TemplateBinding Header}"
                                                       ContentCharacterCasing="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ControlsHelper.ContentCharacterCasing)}"
                                                       ContentStringFormat="{TemplateBinding HeaderStringFormat}"
                                                       ContentTemplate="{TemplateBinding HeaderTemplate}"
                                                       ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"
                                                       FontSize="{TemplateBinding Controls:ControlsHelper.HeaderFontSize}"
                                                       FontStretch="{TemplateBinding Controls:ControlsHelper.HeaderFontStretch}"
                                                       FontWeight="{TemplateBinding Controls:ControlsHelper.HeaderFontWeight}"
                                                       RecognizesAccessKey="True"
                                                       SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                       UseLayoutRounding="False">
                                <TextElement.Foreground>
                                    <MultiBinding Converter="{x:Static Converters:BackgroundToForegroundConverter.Instance}">
                                        <Binding Mode="OneWay"
                                                 Path="Background"
                                                 RelativeSource="{RelativeSource TemplatedParent}" />
                                        <Binding Mode="OneWay"
                                                 Path="(Controls:GroupBoxHelper.HeaderForeground)"
                                                 RelativeSource="{RelativeSource TemplatedParent}" />
                                    </MultiBinding>
                                </TextElement.Foreground>
                            </Controls:ContentControlEx>
                        </Border>
                        <Border Grid.Row="1"
                                Background="Transparent"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness, Converter={StaticResource ThicknessBindingConverter}, ConverterParameter={x:Static Converters:ThicknessSideType.Top}}"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                UseLayoutRounding="True">
                            <ContentPresenter Margin="{TemplateBinding Padding}"
                                              Content="{TemplateBinding Content}"
                                              ContentTemplate="{TemplateBinding ContentTemplate}"
                                              Cursor="{TemplateBinding Cursor}"
                                              UseLayoutRounding="False" />
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
