Windows 或应用级别的 WPF 样式产生不同的结果

WPF Style at Windows or App level yields different results

我正在尝试学习 WPF 中的样式,遇到了一件有趣的事情:

在应用程序或(主要)window 级别应用样式时存在差异。

当我在 App.xaml 中定义以下资源时:

<Application.Resource>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="FontStyle" Value="Italic" />
    </Style>

    <Style TargetType="{x:Type GroupBox}">
        <Setter Property="FontWeight" Value="Bold" />
    </Style>
<Application.Resource>

GroupBox 标题为粗体 斜体。

当我改为在 MainWindow.xaml 中定义样式时:

<Window.Resources>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="FontStyle" Value="Italic" />
    </Style>

    <Style TargetType="{x:Type GroupBox}">
        <Setter Property="FontWeight" Value="Bold" />
    </Style>
</Window.Resources>

GroupBox 标题框只有粗体,没有斜体。

任何人都可以解释这种行为吗?

在选择 TextBlock 时,您发现了一些东西。 TextBlock 不是从 Control 派生的,因此行为略有不同。

参见:

there is a curious rule in WPF implicit styles are only inherited across template boundaries by elements which inherit from the Control class

我想你可以补充一点,"unless it is globally specified in App.xaml"。

更新:

根据评论,这里是一个 GroupBox 视觉树,取自 Snoop。