Is there a way in XAML to set a style that will apply to all controls? For example below I would like to put a margin on all my controls. I can add a style for each type by change the TargetType to Button, CheckBox, etc. Instead, I would like to set it up like I have below where I set the style on all types that inherit from Control:
<UserControl x:Class="MyPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Height="300" Width="300">
<UserControl.Resources>
<Style TargetType="Control">
<Setter Property="Margin" Value="3"/>
</Style>
</UserControl.Resources>
<StackPanel>
<Button>My Button</Button>
<CheckBox>My Checkbox</CheckBox>
</StackPanel>
</UserControl>
There is no way to do this. However, you can define a base style and inherit from that:
<Style x:Key="BaseStyle">
<Setter Property="FrameworkElement.Margin" Value="3"/>
</Style>
<Style TargetType="Button" BasedOn="{StaticResource BaseStyle}">
</Style>
<Style TargetType="TextBox" BasedOn="{StaticResource BaseStyle}">
</Style>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With