65
我可以指定適用於所有元素的樣式嗎?我試圖如何定位所有控件(WPF樣式)
<Style TargetType="Control">
<Setter Property="Margin" Value="0,5" />
</Style>
但它確實沒有什麼
我可以指定適用於所有元素的樣式嗎?我試圖如何定位所有控件(WPF樣式)
<Style TargetType="Control">
<Setter Property="Margin" Value="0,5" />
</Style>
但它確實沒有什麼
的Style
您創建僅定位到從Control
獲得Control
,而不是元素。當你沒有設置x:Key
它隱式設置爲TargetType
,所以在你的案例x:Key="{x:Type Control}"
。
沒有任何直接指定Style
的方法,其目標是從Style
的TargetType
派生的所有元素。你有其他的選擇。
如果您有以下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}" ...>
謝謝!正是我需要的! – 00jt 2012-11-13 15:28:54
我想知道爲什麼我的風格與目標類型的'FrameworkElement'沒有被應用到我的所有控件 - 這回答了這個問題! – qJake 2014-07-16 13:01:26
真的很蹩腳,我必須指定放置在窗口上的所有類型的控件的樣式,而不僅僅是對於所有通用的根類型的東西? – 2016-08-30 09:05:47