Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basing A Style Off A Dynamic Resource

Tags:

c#

wpf

It seems 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.

like image 396
Robert Avatar asked Oct 19 '25 13:10

Robert


2 Answers

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.

like image 106
Wonko the Sane Avatar answered Oct 22 '25 04:10

Wonko the Sane


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>
like image 29
Mr.B Avatar answered Oct 22 '25 03:10

Mr.B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!