我有一个奇怪的场景,涉及覆盖 WPF 中的默认样式并将它们应用于子类。这不可能吗?例如,我有以下代码:
<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>
使用此代码隐藏:
namespace TestWPFStyling
{
public partial class MainWindow : Window
{
}
public class SomeLabel : Label
{
}
public class YellowLabel : SomeLabel
{
}
}
我希望 StackPanel 中的控件 YellowLabel 的颜色为蓝色,外部的颜色为红色,但它们都是黑色的。有没有办法让子类采用其父类的默认样式?