【问题标题】:Basing A Style Off A Dynamic Resource基于动态资源的样式
【发布时间】:2010-09-22 17:46:08
【问题描述】:

似乎不允许这样的事情。有什么解决方法吗?

  <Style x:Key=MyDerivedStyle TargetType="{x:Type Button}"
         BasedOn="{DynamicResource GlobalButtonStyle}" />       

  <Style x:Key="GlobalLabelStyle" TargetType="{x:Type Button}">

我得到错误: 不能在“Style”类型的“BasedOn”属性上设置“DynamicResourceExtension”。只能在 DependencyObject 的 DependencyProperty 上设置“DynamicResourceExtension”。

如果我将其更改为 StaticResource,则该样式不会出现在我的控件中。

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    这里有两个问题:

    首先,您的全局样式需要出现在派生样式之前(在同一资源部分中,或者在尝试定义派生样式之前通过合并到适当的 ResourceDictionary 中。

    另外,您需要在按钮中明确定义样式:

    <Button x:Name="btnOne"
            Style="{StaticResource MyDerivedStyle}"
            Content="Derived" />
    

    请注意,在这种情况下,您不是在创建动态资源(即需要重新加载的资源)。它是静态的,就像用于 BasedOn 的 Style 一样。

    【讨论】:

    • 为了进一步澄清,合并字典确实遵循“声明顺序”系统。如果尚未定义资源,则不能使用它。将根样式放在使用它的样式之上。
    【解决方案2】:

    首先你需要放置Based Style,然后是使用这个Bass Style的Style:

    <Style x:Key="ComboBoxItemStyleSpecial"
           BasedOn="{StaticResource ComboBoxItemStyleDefault}"
           TargetType="{x:Type ComboBoxItem}">
        <Setter Property="BorderBrush"
                Value="Lime" />
        <Setter Property="BorderThickness"
                Value="3" />
    </Style>
    <Style x:Key="ComboBoxItemStyleSpecialFont"
           BasedOn="{StaticResource ComboBoxItemStyleSpecial}"
           TargetType="{x:Type ComboBoxItem}">
        <Setter Property="FontSize"
                Value="40" />
        <Setter Property="FontFamily"
                Value="Aharoni" />
    </Style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 2019-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多