如何覆盖其标题的WPF GroupBox禁用(即灰色)颜色?

时间:2011-04-29 15:25:29

标签: wpf xaml

我已经按照之前答案的建议来设置包含HeaderTemplate的全局GroupBox样式。该样式适用于启用受影响的GroupBox对象的情况。禁用受影响的GroupBox对象时,我只能更改边框颜色。禁用GroupBox对象时,我不想要默认的灰色。我的GroupBox样式如下所示:

    <!-- This is the color brush I am trying to set on the header text -->
    <SolidColorBrush x:Key="moDisabledFGColor" Color="DarkBlue" Opacity="0.5" />

    <Style TargetType="{x:Type GroupBox}">
        <Setter Property="BorderThickness" Value="2" />
        <Setter Property="BorderBrush" Value="{StaticResource moFGColor}"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="Foreground" Value="{StaticResource moFGColor}" />
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock x:Name="HeaderText" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type GroupBox}}, Path=Header}" FontWeight="Bold"
                                Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}, Path=Foreground}" />
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type GroupBox}}}" Value="False">
                            <Setter TargetName="HeaderText" Property="Foreground" Value="{StaticResource moDisabledFGColor}"/>
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="BorderBrush" Value="{StaticResource moDisabledFGColor}" />
                <Setter Property="Foreground" Value="{StaticResource moDisabledFGColor}" />
            </Trigger>
        </Style.Triggers>
    </Style>

0 个答案:

没有答案