user1632018
user1632018

Reputation: 2555

Do I need to create a new style every time I want to change an attribute of a style in WPF?

I have two buttons with a custom style in WPF. Right now both buttons are the same color, but I have decided I want to make one button blue. Now my question is do I need to create another style even though the change I wan't is so simple? It seems a little redundant to have to repeat all of the code for one more button just because I want it a different color.

When I try to override the buttons background color, it removes the applied style from the button. I believe it should be like CSS where you can override an attribute while keeping the others the same, although that isn't the case. I don't need it again so I don't think I need a custom control. Is there any way to simply change the background color without repeating all of the style code?

Upvotes: 0

Views: 69

Answers (1)

robertos
robertos

Reputation: 1796

You can create a Style BasedOn another. See MSDN: http://msdn.microsoft.com/en-us/library/system.windows.style.basedon(v=vs.110).aspx

<Style x:Key="Style1">
  <Setter Property="Control.Background" Value="Yellow"/>
</Style>

<Style x:Key="Style2" BasedOn="{StaticResource Style1}">
  <Setter Property="Control.Foreground" Value="Blue"/>
</Style>

Upvotes: 7

Related Questions