tags:

views:

251

answers:

1

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>
+4  A: 

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>

HTH, Kent

Kent Boogaart