I have a weird scenario involving overriding default styles in WPF and having them apply to subclasses. Is this not possible? For example, I have the following code:
<Window x:Class="TestWPFStyling.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:testWpfStyling="clr-namespace:TestWPFStyling"
Title="MainWindow" Height="350" Width="525" Background="White">
<Window.Resources>
<Style TargetType="testWpfStyling:SomeLabel">
<Setter Property="Foreground" Value="Red" />
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<testWpfStyling:SomeLabel Grid.Row="0">This should be red.</testWpfStyling:SomeLabel>
<testWpfStyling:YellowLabel Grid.Row="1">This should be red.</testWpfStyling:YellowLabel>
<StackPanel Grid.Row="2">
<StackPanel.Resources>
<Style TargetType="testWpfStyling:SomeLabel">
<Setter Property="Foreground" Value="Blue" />
</Style>
</StackPanel.Resources>
<testWpfStyling:SomeLabel>This should be blue.</testWpfStyling:SomeLabel>
<testWpfStyling:YellowLabel>This should be blue.</testWpfStyling:YellowLabel>
</StackPanel>
</Grid>
</Window>
With this code-behind:
namespace TestWPFStyling
{
public partial class MainWindow : Window
{
}
public class SomeLabel : Label
{
}
public class YellowLabel : SomeLabel
{
}
}
I would expect the control YellowLabel in the StackPanel to have a color of Blue and the one outside to be red, however both of them are black. Is there a way for a subclass to take on the default style of its parent?