Inherited Xaml Styles

Is there any way in XAML to set a style that will apply to all controls? For example, below I would like to put a margin in all my controls. I can add a style for each type by changing TargetType to Button, CheckBox, etc. Instead, I would like to configure it as shown below, where I set the style for 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> 
+4
source share
1 answer

There is no way to do this. However, you can define a base style and inherit from it:

 <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> 
+15
source

All Articles