Search code examples
c#wpfmahapps.metro

Inherit style from Mahapps for custom GroupBox Header in WPF


I need a custom header for my GroupBox element. Now the problem is, when I switch the style in my App, the label object of the CheckBox in the header doesn't change the foreground according to the new theme style.

How can I do to inherit the style from the current theme?

I tried to inherit the style from the StaticRessource LabelTextBrush, however the label remains always black. Maybe it's the wrong ressource?

<GroupBox x:Name="gpDetailView" Grid.Column="1" Margin="5" Grid.Row="3" Grid.ColumnSpan="2">
    <GroupBox.Header>
        <DockPanel>
            <CheckBox x:Name="ckbState" Content="Ersatzteil aktiv" Foreground="{DynamicResource LabelTextBrush}">
                <CheckBox.Resources>
                    <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource {x:Type CheckBox}}">
                        <Setter Property="Foreground" Value="{DynamicResource LabelTextBrush}"/>
                    </Style>
                </CheckBox.Resources>
            </CheckBox>
        </DockPanel>
    </GroupBox.Header>
    <Grid>


    </Grid>
</GroupBox>

Any help is appreciated! Thanks!


Solution

  • You could either try the IdealForegroundColorBrush or rely on the BackgroundToForegroundConverter to get you an "ideal" foreground brush:

    <CheckBox x:Name="ckbState" Content="Ersatzteil aktiv"
              xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
              xmlns:Converters="clr-namespace:MahApps.Metro.Converters;assembly=MahApps.Metro">
        <CheckBox.Foreground>
            <MultiBinding Converter="{x:Static Converters:BackgroundToForegroundConverter.Instance}">
                <Binding Mode="OneWay" Path="Background" RelativeSource="{RelativeSource AncestorType=GroupBox}" />
                <Binding Mode="OneWay" Path="(Controls:GroupBoxHelper.HeaderForeground)" RelativeSource="{RelativeSource AncestorType=GroupBox}" />
            </MultiBinding>
        </CheckBox.Foreground>
    </CheckBox>