1

I have xaml styles that have different target types but are otherwise identical. Is there a way I could cut out the duplication and define the style only once?

<Style TargetType="TextBlock">            
    <Setter Property="Height" Value="{StaticResource ElementHeight}"/>
    <Setter Property="MinWidth" Value="{StaticResource ElementMinWidth}"/>
    <Setter Property="Margin" Value="{StaticResource ElementMargin}"/>
</Style>

<Style TargetType="TextBox">
    <Setter Property="Height" Value="{StaticResource ElementHeight}"/>
    <Setter Property="MinWidth" Value="{StaticResource ElementMinWidth}"/>
    <Setter Property="Margin" Value="{StaticResource ElementMargin}"/>
</Style>

<Style TargetType="ComboBox">
    <Setter Property="Height" Value="{StaticResource ElementHeight}"/>
    <Setter Property="MinWidth" Value="{StaticResource ElementMinWidth}"/>
    <Setter Property="Margin" Value="{StaticResource ElementMargin}"/>
</Style>
Connell.O'Donnell
  • 3,603
  • 11
  • 27
  • 61

1 Answers1

2

You could use style inheritance with the help of Style.BasedOn.

First define the base style:

   <Style x:Key="BaseStyle" TargetType="FrameworkElement">
        <Setter Property="Height" Value="80"/>
        <Setter Property="MinWidth" Value="80"/>
        <Setter Property="Margin" Value="80"/>
    </Style>

Then "inherit" styles from that for the controls you want:

    <Style TargetType="TextBlock" BasedOn="{StaticResource BaseStyle}"/>
    <Style TargetType="TextBox" BasedOn="{StaticResource BaseStyle}"/>
    <Style TargetType="ComboBox" BasedOn="{StaticResource BaseStyle}"/>
Mikael Koskinen
  • 12,306
  • 5
  • 48
  • 63