简体   繁体   中英

WPF GroupBox Style not being applied on custom control

I have created a custom control so simply draw the border of a geroupbox in red if there is an error. for some reason when I use the control the foreground colour doesn't get set correctly it defaults to white instead of green. My code is as follows:

public class ErrorGroupBox : GroupBox
{
    static ErrorGroupBox()
    {
    }

    public bool ErrorState
    {
        get { return (bool)GetValue(ErrorStateProperty); }
        set { base.SetValue(ErrorStateProperty, value); }
    }

    public static readonly DependencyProperty ErrorStateProperty =
      DependencyProperty.Register("ErrorState", typeof(bool), typeof(ErrorGroupBox), new UIPropertyMetadata(false));
}

The style is:

<Style x:Name="ErrorGroupBox" TargetType="local:ErrorGroupBox">
    <Setter Property="Foreground" Value="Green"/>
    <Setter Property="BorderThickness" Value="2"/>
    <Setter Property="FontFamily" Value="Tahoma"/>
    <Setter Property="FontSize" Value="13"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="GroupBox">
                <Grid SnapsToDevicePixels="True" Margin="5">
                    <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="1    " Grid.ColumnSpan="4" 
                         Grid.Row="1" Grid.RowSpan="3" HorizontalAlignment="Stretch"/>-->

                    <!--The Title-->
                    <Border x:Name="Header" Grid.Column="2" Grid.RowSpan="2" HorizontalAlignment="Left" 
                        Padding="3,1,3,0" VerticalAlignment="Stretch" >
                        <Border.Effect>
                            <DropShadowEffect BlurRadius="5" Direction="334"/>
                        </Border.Effect>

                        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                             Content="{TemplateBinding Header}" 
                             ContentSource="Header" 
                             ContentStringFormat="{TemplateBinding HeaderStringFormat}" 
                             ContentTemplate="{TemplateBinding HeaderTemplate}" 
                             RecognizesAccessKey="True" Height="Auto" 
                             VerticalAlignment="Center"
                             HorizontalAlignment="Center"
                             OpacityMask="#FF3844BD" Margin="0,1,0,0">
                        </ContentPresenter>
                    </Border>

                    <ContentPresenter Margin="5" 
    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
    Content="{TemplateBinding Content}" 
    ContentStringFormat="{TemplateBinding ContentStringFormat}" 
    ContentTemplate="{TemplateBinding ContentTemplate}" 
    Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="2" />
                    <Border BorderBrush="#BFBFBF" BorderThickness="{TemplateBinding BorderThickness}" 
                        CornerRadius="4" Grid.ColumnSpan="3" Grid.Row="1" Grid.RowSpan="3" RenderTransformOrigin="0.5,0.5" Margin="0">
                        <Border.OpacityMask>
                            <MultiBinding ConverterParameter="7" UpdateSourceTrigger="Default">
                                <MultiBinding.Converter>
                                    <BorderGapMaskConverter/>
                                </MultiBinding.Converter>
                                <Binding Path="ActualWidth" ElementName="Header"/>
                                <Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}"/>
                                <Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}"/>
                            </MultiBinding>
                        </Border.OpacityMask>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>                    
                    <Trigger Property="local:ErrorGroupBox.ErrorState" Value="True">
                        <Setter Property="Foreground" Value="Green"/>
                    </Trigger>
                    <Trigger Property="local:ErrorGroupBox.ErrorState" Value="False">
                        <Setter Property="Foreground" Value="Green"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I use the control as follows:

    <controls:ErrorGroupBox ErrorState="{Binding Error}" Header="Test Type" Margin="20,20,0,0" Name="groupBox1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="226" Grid.Column="0">

If this is a default theme style you need to specify a different metadata type in your static constructor:

public class ErrorGroupBox : GroupBox
{
    static ErrorGroupBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ErrorGroupBox), new FrameworkPropertyMetadata(typeof(ErrorGroupBox )));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM