tags:

views:

216

answers:

1

Please don't get caught up in my example, just bear with me for the sake of the question:

In my WPF application, if I wanted all TextBoxes to have a "green" background, I would easily set it as such in my Application.Resources.

<Style TargetType="TextBox">
 <Setter Property="Background" Value="Green" />
</Style>

This works PERFECTLY... (thank you WPF). However, if I had a TextBox somewhere in my application that I wanted to append a little more styling to... I LOSE my green background.

Example:

<TextBox>
 <TextBox.Style>
  <Style>
   <Style.Triggers>
    <Trigger Property="TextBox.IsMouseOver" Value="True">
     <Setter Property="TextBox.Foreground" Value="Red" />
    </Trigger>
  </Style.Triggers>
  </Style>
 </TextBox.Style>
</TextBox>

That TextBox will correctly have the red Foreground when the mouse is over, but the green Background is completely lost.

So, the question is: How do I tell WPF NOT to completely wipe out all styling that came from above just because I have a simple, non-conflicting, oh so tiny style added to a control somewhere?

+3  A: 

You can inherit already overridden styles using "BasedOn" in the Style declaration.

In the declaration for your second style, try this:

<TextBox>
 <TextBox.Style>
  <Style BasedOn="{StaticResource {x:Type TextBox}}">
   <Style.Triggers>
    <Trigger Property="TextBox.IsMouseOver" Value="True">
     <Setter Property="TextBox.Foreground" Value="Red" />
    </Trigger>
  </Style.Triggers>
  </Style>
 </TextBox.Style>
</TextBox>

You can also base the style on a named style,

<Style x:Key=MyNamedStyle>
</Style>

<Style BasedOn="{StaticResource MyNamedStyle}" >
</Style>
Guy Starbuck
True, but is basically like saying "you have to re-add styles to everything you want to override". :( - Good answer, but I hate that solution.
Timothy Khouri
I think of it more as a cascading style thing. There may be places you DON'T want to inherit your other style overrides, or where you have different sets of styles for the same type of control in different scenarios. This way gives you more control, so I don't mind it so much.
Guy Starbuck
@Timothy, BasedOn attribute means extending the style from specified type, even in OOPS whenever you inherit a class, technically at compilation level you re-add all functionalities of base class into new class. Inheritance means retaining/copying everything and overriding some part.
Akash Kava
@Guy - I'd like it if it was cascading... but it's not. Trying to add more styles 100% OVERRIDES THE PREVIOUS STYLES. So, to recap, they don't cascade (aka append), they override (aka replace).
Timothy Khouri
@Akash - Your reasoning sounds good, but the bottom line comes down to this. Do I have to tell all 15 developers on my development team to make sure to add "ooh, please don't forget to inhert!" to every control that they add functional styling to? - That's horrible and is more work than it's worth.
Timothy Khouri
Well, you can make it cascade if you name each of your styles, including the inherited ones, then have "child" styles inherit from other inherited ones -- I see your point, though, it's certainly a lot to manage independently of the code.
Guy Starbuck