WPF: Inherit Style From Style. BasedOn Is Your Friend

So another quick one.  Say you have a style for creating round buttons, but you don’t want to include a size, how a size be applied without touching the original style?  Well you could just adjust the properties right on the button, but what if you want something a little more reusable?  It’s actually pretty simple, just have one style “inherit” the other.  Well that’s where BasedOn comes in.

Say you have a style named RoundButton.

    <Style x:Key="GlassButton" TargetType="{x:Type Button}">
      ...
    </Style>

All you have to do is create a new style:

    <Style x:Key="HeaderButton" BasedOn="{StaticResource GlassButton}" TargetType="{x:Type Button}">
        <Setter Property="Height" Value="20" />
        <Setter Property="Width" Value="20" />
    </Style>

As you can see, it’s pretty simple using the BasedOn keyword. Now when the HeaderButton style is applied to the button, it will take on the properties of the RoundButton style and the HeaderButton one. Nice huh?