WPF style basedon current

13,045

Solution 1

You need to do it in this way only, there is no shortcut to do this, you have to set BasedOn attribute atleast.

Solution 2

if you store all your resources in seperate assemblies from your ui then you can use themes to dynamically change them at runtime.

themes in seperate assembly

loading different themes at runtime

Share:
13,045
adrianm
Author by

adrianm

Updated on July 14, 2022

Comments

  • adrianm
    adrianm almost 2 years

    Is there a way to create a style that extends the current style, i.e. not a specific style?

    I have a WPF application where I create styles to set some properties like borders or validation.

        <Style TargetType="{x:Type Button}"  
               BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="Padding" Value="5,2,5,2"/>
        </Style>
    

    Now I want to try out some Themes to see which one works best for my application. Problem is that to do that I need to change the BasedOn property on all my Styles to the style in the Theme.

        <Style TargetType="{x:Type Button}"  
               BasedOn="="{x:Static ns:SomeTheme.ButtonStyle}">">
            <Setter Property="Padding" Value="5,2,5,2"/>
        </Style>
    

    I can do it via search/replace but it would be nice if it could be done dynamicly.

  • adrianm
    adrianm over 14 years
    Error message: "Property 'BasedOn' does not support values of type 'DynamicResourceExtension'."