It is common to need a base style that defines properties/values shared between multiple styles belonging to the same control, especially for something like TextBlock
. This is accomplished by using the BasedOn
property. Values are inherited and then can be overridden.
<Style x:Key="BaseTextBlockStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="12"/>
<Setter Property="Foreground" Value="#FFBBBBBB" />
<Setter Property="FontFamily" Value="Arial" />
</Style>
<Style x:Key="WarningTextBlockStyle"
TargetType="TextBlock"
BasedOn="{StaticResource BaseTextBlockStyle">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FontWeight" Value="Bold" />
</Style>
In the above example, any TextBlock
using the style WarningTextBlockStyle
would be presented as 12px Arial in red and bold.
Because implicit styles have an implicit x:Key
that matches their TargetType
, you can inherit those as well:
<!-- Implicit -->
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="12"/>
<Setter Property="Foreground" Value="#FFBBBBBB" />
<Setter Property="FontFamily" Value="Arial" />
</Style>
<Style x:Key="WarningTextBlockStyle"
TargetType="TextBlock"
BasedOn="{StaticResource {x:Type TextBlock}}">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FontWeight" Value="Bold" />
</Style>