开发者

WPF Styles - Please help me understand why this works the way it does

开发者 https://www.devze.com 2023-03-11 00:05 出处:网络
<Style x:Key=\"MyStyle\"> <Setter Property=\"Window.Background\" Value=\"Orange\"/> </Style>
<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

0

精彩评论

暂无评论...
验证码 换一张
取 消