2010-11-09 18 views

回答

85

Style您創建僅定位到從Control獲得Control,而不是元素。當你沒有設置x:Key它隱式設置爲TargetType,所以在你的案例x:Key="{x:Type Control}"

沒有任何直接指定Style的方法,其目標是從StyleTargetType派生的所有元素。你有其他的選擇。

如果您有以下Style

<Style x:Key="ControlBaseStyle" TargetType="{x:Type Control}"> 
    <Setter Property="Margin" Value="50" /> 
</Style> 

您可以針對所有Buttons例如

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource ControlBaseStyle}"/> 

或直接的任何元素,例如在使用樣式Button

<Button Style="{StaticResource ControlBaseStyle}" ...> 
+3

謝謝!正是我需要的! – 00jt 2012-11-13 15:28:54

+0

我想知道爲什麼我的風格與目標類型的'FrameworkElement'沒有被應用到我的所有控件 - 這回答了這個問題! – qJake 2014-07-16 13:01:26

+0

真的很蹩腳,我必須指定放置在窗口上的所有類型的控件的樣式,而不僅僅是對於所有通用的根類型的東西? – 2016-08-30 09:05:47