Basing A Style Off A Dynamic Resource
It se开发者_如何转开发ems something like this is not allowed. Any workaround?
<Style x:Key=MyDerivedStyle TargetType="{x:Type Button}"
BasedOn="{DynamicResource GlobalButtonStyle}" />
<Style x:Key="GlobalLabelStyle" TargetType="{x:Type Button}">
I get the error: A 'DynamicResourceExtension' cannot be set on the 'BasedOn' property of type 'Style'. A 'DynamicResourceExtension' can only be set on a DependencyProperty of a DependencyObject.
If I change it to StaticResource, the style does not appear in my control.
Two issues here:
First, your global style needs to appear before your derived style (either in the same resources section, or by merging in the appropriate ResourceDictionary before attempting to define the derived style.
Also, you need to explicitly define the Style in your button:
<Button x:Name="btnOne"
Style="{StaticResource MyDerivedStyle}"
Content="Derived" />
Note that in this case you aren't creating a Dynamic Resource (i.e. one that needs to be reloaded). It is static, as a Style that is being used for a BasedOn needs to be.
Firstly you need to place Based Style and after that the Style that using this Bass 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>
精彩评论