tags:

views:

37

answers:

3

Hi all,

Is it possible to override styles in other styles. My best description will be some non working code:

<Style x:Key="SpecialFont" TargetType="Label">
    <Setter Property="Foreground" Value="Red" />
    <Setter Property="FontSize" Value="28" />
</Style>

<Style TargetType="GroupBox">
    <Setter Property="GroupBox.Resources">
        <Setter.Value>
            <Style x:Key="SpecialFont" TargetType="Label">
                <Setter Property="FontSize" Value="74" />
            </Style>
        </Setter.Value>
    </Setter>
</Style>

The idea is that I will define a style to my "special text", the font which by default is red and have a size at 28, but if the label is placed in a groupbox it should have the size at 74, but maintain the red color. How is this possible? I would prefer to have to same style-key in my xaml, and not create a style based on another, e.g. SpecialFontBig based on SpecialFont.

A: 

Styles can be based on other styles -> http://msdn.microsoft.com/en-us/library/system.windows.style.basedon.aspx

<Style x:Key="Style1">
  <Setter Property="Control.Background" Value="Yellow"/>
</Style>

<Style x:Key="Style2" BasedOn="{StaticResource Style1}">
  <Setter Property="Control.Foreground" Value="Blue"/>
</Style>
rudigrobler
Ups... double post, but as I wrote above I don't want to have two different styles, but just override some values in the style, if it is used in the groupbox.
kahr
A: 

Okay... Another explanation.

I want result like this:

<Style x:Key="BaseFont" TargetType="Label">
    <Setter Property="Foreground" Value="White" />
</Style>

<Style x:Key="Font1" TargetType="Label" BasedOn="{StaticResource BaseFont}">
    <Setter Property="FontSize" Value="10" />
</Style>

<Style x:Key="Font2" TargetType="Label" BasedOn="{StaticResource BaseFont}">
    <Setter Property="FontSize" Value="20" />
</Style>

<Style x:Key="Font3" TargetType="Label" BasedOn="{StaticResource BaseFont}">
    <Setter Property="FontSize" Value="30" />
</Style>

<Style x:Key="Font1Red" TargetType="Label" BasedOn="{StaticResource Font1}">
    <Setter Property="Foreground" Value="Red" />
</Style>

<Style x:Key="Font2Red" TargetType="Label" BasedOn="{StaticResource Font2}">
    <Setter Property="Foreground" Value="Red" />
</Style>

<Style x:Key="Font3Red" TargetType="Label" BasedOn="{StaticResource Font3}">
    <Setter Property="Foreground" Value="Red" />
</Style>

Where FontX is used outside my groupboxes, and FontXRed is used inside them. Is it possible to overrule this foreground, without making a lot of FontXRed styles? For example something like:

<Style x:Key="BaseFont" TargetType="Label">
    # IF INSIDE A GROUPBOX
    <Setter Property="Foreground" Value="Red" />
    # ELSE
    <Setter Property="Foreground" Value="White" />
</Style>
kahr
I have many styles (not only three) which inherit from the basefont, and therefore it will be a lot of extra styles, by doing the color-switch in this way.
kahr
Please don't extend your question by answering. Just edit your original question.
Robaticus
A: 

I have created a new GroupBox, which solved my problem okay:

class MyGroupBox : GroupBox
{
    public MyGroupBox()
    {
        var newForegroundSetter = new Setter(ForegroundProperty, Brushes.Black);

        var stylesToUpdate = new List<string>
                                 {
                                     "TextBlockShared",
                                     "SmallFontTextBlock",
                                     "MediumFontTextBlock",
                                     "LargeFontTextBlock",

                                     "FontControlShared",
                                     "SmallFontControl",
                                     "SmallFontHeaderControl",
                                     "MediumFontControl",
                                     "MediumFontHeaderControl",
                                     "LargeFontControl",
                                     "LargeFontHeaderControl",

                                     "SmallButton",
                                     "MediumButton",
                                     "LargeButton",
                                 };

        foreach (var styleKey in stylesToUpdate)
        {
            var existingStyle = FindResource(styleKey) as Style;
            if (existingStyle == null) continue;

            var newStyle = new Style(existingStyle.TargetType, existingStyle);
            newStyle.Setters.Add(newForegroundSetter);

            Resources.Add(styleKey, newStyle);
        }
    }
}
kahr