How can I create a Style deriving from another Style

Platform: WPF| Category: Styles and Templates

Perhaps you want your two ‘TextBlock’ elements to share some property values such as the FontFamily and the centered HorizontalAlignment, but you also want the text ‘My Pictures’ to have some additional properties. You can do that by creating a new style that is based on the first style, as shown here.

[XAML]

<Window.Resources>

...

<!--A Style that extends the previous MyBaseStyle Style-->
<!--This is a 'named style' with an x:Key of MyDerivedStyle-->
<Style BasedOn='{StaticResource {x:Type MyBaseStyle}}'
       TargetType='TextBlock'
       x:Key='MyDerivedStyle'>
  <Setter Property='FontSize' Value='26'/>
  <Setter Property='Foreground'>
  <Setter.Value>
      <LinearGradientBrush StartPoint='0.5,0' EndPoint='0.5,1'>
        <LinearGradientBrush.GradientStops>
          <GradientStop Offset='0.0' Color='#90DDDD' />
          <GradientStop Offset='1.0' Color='#5BFFFF' />
        </LinearGradientBrush.GradientStops>
      </LinearGradientBrush>
    </Setter.Value>
  </Setter>
</Style>

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.