How to extend instead of overriding WPF Styles How to extend instead of overriding WPF Styles wpf wpf

How to extend instead of overriding WPF Styles


I had the same problem. I used Zack's answer and improved it like following so if you don't specify a style the overridden default is still taken in account. It's basically what you would have done but just once in the ResourceDictionary.

<Window x:Class="TestWpf.RandomStuffWindow"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    Title="Random Stuff Window">  <Window.Resources>    <ResourceDictionary>      <!-- Default Label style definition -->      <Style TargetType="{x:Type Label}">        <Setter Property="Foreground" Value="Green" />      </Style>      <!-- Extending default style -->      <Style TargetType="{x:Type Label}"              x:Key="LargeGreenForegroundLabel"              BasedOn="{StaticResource {x:Type Label}}">        <Setter Property="FontSize" Value="28" />      </Style>    </ResourceDictionary>  </Window.Resources>  <StackPanel>    <Button Click="Button_Click">Click</Button>    <Label Content="GreenForegroundLabel" /> <!-- Uses default style -->    <Label Style="{StaticResource LargeGreenForegroundLabel}"            Content="LargeGreenForegroundLabel" />  </StackPanel></Window>


Wpf has different levels of styles, that are applied in order of global > local. A style set directly on a control will override a style set globally, like in your example. I was trying to find a list of all the different places that a control looks for its styles but I cannot find one at the moment. As far as I know, you will have to use the BasedOn property to inherit a style and not completely override the properties of that style with the style you set locally.

Here is an example of a resource dictionary that has styles based on another style, so that you don't have do repeat the BasedOn binding over and over, you can just set the style on the specific element you want to have that style.

<Window x:Class="TestWpf.RandomStuffWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="Random Stuff Window">  <Window.Resources>    <ResourceDictionary>      <Style TargetType="{x:Type Label}"              x:Key="GreenForegroundLabel">        <Setter Property="Foreground" Value="Green" />      </Style>      <Style TargetType="{x:Type Label}"              x:Key="LargeGreenForegroundLabel"              BasedOn="{StaticResource GreenForegroundLabel}">        <Setter Property="FontSize" Value="28" />      </Style>    </ResourceDictionary>  </Window.Resources>  <StackPanel>    <Button Click="Button_Click">Click</Button>    <Label Style="{StaticResource GreenForegroundLabel}"            Content="GreenForegroundLabel" />    <Label Style="{StaticResource LargeGreenForegroundLabel}"            Content="LargeGreenForegroundLabel" />  </StackPanel></Window>