2011-06-05 76 views
5
<Style x:Key="MyStyle"> 
    <Setter Property="Window.Background" Value="Orange"/> 
</Style> 

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

为什么按钮实际上会得到橙色背景,如果setter被指定为Window.Background?WPF样式 - 请帮我理解它为什么这样工作

但这给TextBlock的橙色背景:

<TextBlock Style="{StaticResource MyStyle}"/> 

感谢

回答

5

ButtonWindow都没有定义Background财产,他们都从Control继承。

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

+0

谢谢Sven!但是如何不会使TextBlock的背景变成橙色? TextBlock也继承自Control ... – 2011-06-05 07:53:19

+0

@Gustavo,[TextBlock](http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.aspx)直接从'FrameworkElement'继承,而不是从'Control'。 – 2011-06-05 07:58:47

+0

当然。感谢您指出我的错误Frederic。 – 2011-06-05 08:00:54

2

它的工作原理,因为Background属性附加到Control类都WindowButton有作为祖先

相关问题