Define multiple styles for a UserControl through StaticResource's in WPF

1

I'm starting to work with WPF because of its similarity to HTML. I noticed through my searches that when I use an expression like this Style="{StaticResource MeuStyle}" it looks a lot like a class definition that I have in html ( class="MeuStyle" ).

Is it possible to merge several styles into one StaticResource so that it is applied to the Style of that component? My intention is to work similar to the bootstrap, where I define 2 classes on a button like this:

In HTML

<button class="btn">MeuButton</button>
<button class="btn btn-success">MeuButton</button>
<button class="btn btn-error">MeuButton</button>

In WPF ~ (what I need)

<Button Content="Button" Style="{StaticResource btn}"/>
<Button Content="Button" Style="{StaticResource btn, btnSuccess}"/>
<Button Content="Button" Style="{StaticResource btn, btnError}"/>
    
asked by anonymous 26.01.2016 / 18:54

1 answer

1

You can not specify more than one resource using

StaticResource and neither DynamicResource , but what you can do, and what is also recommended, is to create a style and then create another one that is based on the first property Style.BasedOn ". p>

See an example:

<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>

In the example above Style2 , if applied to an element, it will apply the change of Control.Background of Style1 and change of Control.Foreground of Style2 itself, in this way you get a similar effect with inheritance and that seems to be what you are looking for.

    

28.01.2016 / 02:16