2011-06-05 43 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有作爲祖先

相關問題