Reputation: 9821
I know I can reuse styles by creating a style like:
<Style x:Key="ButtonStyle" TargetType="Button">
<Setter Property="Margin" Value="5" />
</Style>
And applying it:
<Button Style="{StaticResource ButtonStyle}" />
However, I would like to specify a style that automatically applies to e.g. all buttons in the user control, like I would have done in CSS with button { margin: 5px }
. Is this possible?
EDIT: Similar question: Is it possible to set a style in XAML that selectively affects controls?
Upvotes: 0
Views: 83
Reputation: 24453
You can use what are called "implicit styles" by omitting the x:Key
attribute and including only the TargetType
. This will then apply to every control of that type in the scope of the ResourceDictionary
where it is defined.
<Style TargetType="Button">
<Setter Property="Margin" Value="5" />
</Style>
Upvotes: 4
Reputation: 537
By removing the "x:key" Property in the style, it is inherited to all TargetTypes. Which means the same as in your css example.
<Style TargetType="Button">
<Setter Property="Margin" Value="5" />
</Style>
Upvotes: 3