【问题标题】:Override style in style在样式中覆盖样式
【发布时间】:2010-09-08 09:46:13
【问题描述】:

是否可以覆盖其他样式中的样式。我最好的描述是一些不工作的代码:

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

我的想法是我将为我的“特殊文本”定义一个样式,默认情况下字体为红色,大小为 28,但如果标签放在组框中,它的大小应该为 74,但保持红色。这怎么可能?我宁愿在我的 xaml 中使用相同的样式键,而不是基于另一个样式创建样式,例如SpecialFontBig 基于 SpecialFont。

编辑: 好吧...另一种解释。

我想要这样的结果:

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

FontX 在我的分组框之外使用,而 FontXRed 在其中使用。是否可以在不制作大量 FontXRed 样式的情况下推翻这个前景?例如:

<Style x:Key="BaseFont" TargetType="Label">
    # IF INSIDE A GROUPBOX
    <Setter Property="Foreground" Value="Red" />
    # ELSE
    <Setter Property="Foreground" Value="White" />
</Style>

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    样式可以基于其他样式-> 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>
    

    【讨论】:

    • Ups...双重发布,但正如我在上面写的那样,我不想有两种不同的样式,而只是覆盖样式中的一些值,如果它在 groupbox 中使用。跨度>
    【解决方案2】:

    我创建了一个新的 GroupBox,它很好地解决了我的问题:

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

    【讨论】:

      【解决方案3】:

      如果有人偶然发现这个,这里是通常对我有用的技巧。

      基本上,我在另一个样式资源中定义了一个样式。我通常将内部样式基于键引用样式以在其他地方使用它,但您也可以在其中放置一个简单的样式。

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

      【讨论】:

        猜你喜欢
        • 2014-10-12
        • 2020-09-21
        • 2020-05-27
        • 2013-10-18
        • 2020-12-20
        • 2020-08-26
        • 1970-01-01
        • 1970-01-01
        • 2021-11-21
        相关资源
        最近更新 更多