开发者

Override style in style

开发者 https://www.devze.com 2023-01-15 11:38 出处:网络
Is it possible to override styles in other styles. My best description will be some non working code:

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&开发者_开发知识库gt;

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.

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


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>


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);
        }
    }
}


In case someone stumbles upon this, here is the trick that usually works for me.

Basically, I define a style inside another styles resources. I usually base the inner style on a key-referenced style to use it in other places, but you could place a plain simple style in there as well.

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

<Style TargetType="GroupBox">
  <Style.Resources>
    <Style TargetType="Label"
           BasedOn="{StaticResource SpecialFont}" />
  </Style.Resources>
</Style>
0

精彩评论

暂无评论...
验证码 换一张
取 消