从Mahapps继承样式以用于WPF中的自定义GroupBox标头

时间:2019-01-21 05:17:37

标签: c# wpf mahapps.metro

我需要为GroupBox元素添加一个自定义标头。现在的问题是,当我在App中切换样式时,标头中CheckBox的标签对象不会根据新的主题样式更改前景。

如何从当前主题继承样式?

我试图从StaticRessource LabelTextBrush继承样式,但是标签始终保持黑色。也许是错误的资源?

<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>

感谢您的帮助!谢谢!

1 个答案:

答案 0 :(得分:1)

您可以尝试IdealForegroundColorBrush或依靠BackgroundToForegroundConverter来获得“理想的”前景画笔:

<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>