<Style x:Key="MyStyle">
<Setter Property="Window.Background" Value="Orange"/>
</Style>
<Button Content="Ok" Style="{StaticResource MyStyle}"/>
Why does the button actually get an orange background, if the setter is specified as Window.Background?
This does not give the TextBlock an orange background:
<TextBlock Style="{StaticResource MyStyle}"/&开发者_如何学Pythongt;
Thanks
Neither Button
nor Window
actually define the Background
property, they both inherit it from Control
.
So even though you wrote Window.Background
, the setter is actually bound to the property by using the Control.BackgroundProperty
field which also applied to Button
.
It works because the Background
property is attached to Control class which both Window
and Button
have as ancestor
精彩评论