WPF样式 - 请帮助我理解为什么它的工作方式

Gus*_*nti 5 wpf xaml styles

<Style x:Key="MyStyle">
    <Setter Property="Window.Background" Value="Orange"/>
</Style>

<Button Content="Ok" Style="{StaticResource MyStyle}"/>
Run Code Online (Sandbox Code Playgroud)

如果将setter指定为Window.Background,为什么按钮实际上是橙色背景?

这并没有给TextBlock的橙色背景:

<TextBlock Style="{StaticResource MyStyle}"/>
Run Code Online (Sandbox Code Playgroud)

谢谢

Sve*_*ven 5

既没有Button也没有Window实际定义Background属性,它们都继承了它Control.

所以即使你写了Window.Background,setter实际上也是通过使用Control.BackgroundProperty也应用于的字段绑定到属性Button.