Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xaml Inherited Styles

Tags:

styles

wpf

xaml

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>
like image 449
Jake Pearson Avatar asked Dec 07 '22 07:12

Jake Pearson


1 Answers

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>
like image 179
Kent Boogaart Avatar answered Dec 11 '22 07:12

Kent Boogaart