1

In many cases I have "small" styles that I want to apply but still "benefit" from globally set styles.

A small example:

<Windows.Resources>    
    <!-- a "small" style that only modifies a very small detail -->
    <Style x:Key="S1" TargetType="Button">
        <Setter  Property="Background" Value="Yellow"/>
    </Style>
    <!-- the style I want to use in addition-->
    <Style TargetType="Button">
        <Setter  Property="Foreground" Value="Blue"/>
    </Style>
</Windows.Resources>

<!-- this Button should have a yellow background and a blue foreground -->
<Button Style="{StaticResource ResourceKey=S1}">S1</Button>     

If I want to apply two styles, I have a solution based on this SO answer, but it's not applicable here since one style is implicit.

I also cannot make a style based on the other style, since style S1 doesn't know about the automatic style and the automatic style should apply to other controls too that don't use S1.

Community
  • 1
  • 1
Onur
  • 5,017
  • 5
  • 38
  • 54

2 Answers2

2

I recently faced similar issue and I worked it around as follows: I added key BaseButtonStyle to base style which is supposed to be used repeatedly

<Window.Resources>
    <Style x:Key="BaseButtonStyle" TargetType="Button">
        <Setter Property="Foreground" Value="Blue"/>
    </Style>
    <Style x:Key="S1" TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}">
        <Setter Property="Background" Value="Yellow"/>
    </Style>
</Window.Resources>

and in other Windows/UserControls/UI Elements where BaseButtonStyle should be assigned automatically I created

<Window.Resources>
    <Style TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}" />
</Window.Resources>
Maximus
  • 3,458
  • 3
  • 16
  • 27
  • I wanted to merge the "small" style with the implicit style which may depend on the windows version. Although your version may help in other situations, I think it won't in my case. – Onur Jul 07 '15 at 08:17
1

I just discovered a solution that works for me (cause I wanted to keep the default system style but just tweak some minor aspects).

The essence is to base the style on the system style. Here's the essential code part:

<Style TargetType=”TextBox” BasedOn=”{StaticResource {x:Type TextBox}}”>
Onur
  • 5,017
  • 5
  • 38
  • 54