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

<Button Content="Ok" Style="{StaticResource MyStyle}"/>

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

这不会而不是给TextBlock橙色背景:
<TextBlock Style="{StaticResource MyStyle}"/>

谢谢

最佳答案

ButtonWindow都没有实际定义Background属性,它们都从Control继承了它。

因此,即使您编写了Window.Background, setter 实际上仍通过使用Control.BackgroundProperty字段绑定(bind)到该属性,该字段也适用于Button

关于WPF样式-请帮助我理解为什么这样做会起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6241574/

10-15 20:32