5

I'm trying to set multiple styles in a button, in particular I've:

style (1) => Style="{StaticResource VisibleAnimation}"
style (2) => Style="{DynamicResource AccentedSquareButtonStyle}"

A pseudo code:

<Button Content="Invia" Style="{DynamicResource AccentedSquareButtonStyle, VisibleAnimation}" ></Button>

EDIT: POSSIBLE SOLUTION WITH MERGE

<Style TargetType="FrameworkElement" x:Key="VisibleAnimation" BasedOn="{DynamicResource AccentedSquareButtonStyle}">

The compiler underlined the BasedOn line and display me:

Unable to set the properties for DynamicResourceExtension basedon type style. You can set DynamicResourceExtension only for a Dependency Property of a DependencyObject.

How I can achieve this?

Bender
  • 523
  • 6
  • 21
  • Each `FrameworkElement` can only have one `Style` but styles can be [`BasedOn`](https://msdn.microsoft.com/en-us/library/system.windows.style.basedon(v=vs.110).aspx) one another – dkozl Sep 12 '15 at 09:09
  • You need to use `StaticResource`. `BasedOn` is not `DependencyProperty` therefore you cannot use `DynamicResource` – dkozl Sep 12 '15 at 09:53
  • if I make your edit: BasedOn="{StaticResource AccentedSquareButtonStyle}" the compiler underlined all structure and display this error: you can only rely on a style with the kind of target base FrameworkElement – Bender Sep 12 '15 at 09:58
  • `BasedOn` style must be accessible. Most likely you should merge dictionaries and include the one with `AccentedSquareButtonStyle` so you can use it as `StaticResource` – dkozl Sep 12 '15 at 10:06
  • Does this answer your question? [In WPF, Is there any way to combine two Style for one control?](https://stackoverflow.com/questions/6313236/in-wpf-is-there-any-way-to-combine-two-style-for-one-control) – StayOnTarget Aug 16 '21 at 17:28

2 Answers2

7

You could make Style1 basedon Style2 or vise versa, then apply the top level style to your button,

<Window.Resources>
<Style TargetType="Button" x:Key="Style1">
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
<Style TargetType="Button" x:Key="Style2" BasedOn="{StaticResource Style1}">
    <Setter Property="Background" Value="Green"/>
</Style>


but if you don't wan't to change any if your two styles (they are used elsewhere for example) follow this blog post to extend your button style.

SamTh3D3v
  • 9,854
  • 3
  • 31
  • 47
  • The style => Style="{DynamicResource AccentedSquareButtonStyle}" is part of Mahapp metro. I've only Style="{StaticResource VisibleAnimation}" defined in my Window resources... – Bender Sep 12 '15 at 09:40
  • okay, make the VisibleAnimation style based on AccentedSquareButtonStyle, and apply the VisibleAnimation style to your button – SamTh3D3v Sep 12 '15 at 09:45
  • BaseOn needs a StaticResource, it doesn't support Dynamic resources and, you can't make a style that's target a FramworkElement based on a style that target a button ! – SamTh3D3v Sep 12 '15 at 10:00
  • Since you can't change the AccentedSquareButtonStyle style, your VisibleAnimation Style should target a Button as well – SamTh3D3v Sep 12 '15 at 10:02
  • Okay I've created two style ;) – Bender Sep 12 '15 at 10:07
  • Hint: This works only if x:Key=... stands BEFORE TargetType=...!!! WTF ... – IngoB Jun 29 '22 at 10:56
2

You can only set one Style to the control. However you can "merge" styles by using Style.BasedOn property. Example:

<Style TargetType="Button" x:Key="style1">
    <Setter Property="Background" Value="Red"/>
    <!-- some other setters here -->
</Style>

<Style TargetType="Button" BasedOn="{StaticResource style1}">
    <Setter Property="BorderBrush" Value="Green"/>
    <Setter Property="BorderThickness" Value="2"/>
    <!-- some othe setters maybe-->
</Style>

The second style "merges" his setters and setters from style that he derives from. So applying the second style sets the Button Background to Red and BorderBrush to Green. The second style has no x:Key property. This means it would be applied for every Button automatically. You can define only one such style in one ResourceDictionary

bakala12
  • 368
  • 2
  • 15