使用保證金或填充,適用於容器內的範圍:
<StackPanel>
<StackPanel.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin" Value="0,10,0,0"/>
</Style>
</StackPanel.Resources>
<TextBox Text="Apple"/>
<TextBox Text="Banana"/>
<TextBox Text="Cherry"/>
</StackPanel>
編輯:如果你想重新使用兩個容器之間的餘量,可以邊距值轉換爲外部範圍內的資源,fe
<Window.Resources>
<Thickness x:Key="tbMargin">0,10,0,0</Thickness>
</Window.Resources>
,然後參考該值在內部範圍
<StackPanel.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin" Value="{StaticResource tbMargin}"/>
</Style>
</StackPanel.Resources>
的範圍的風格是*真棒*的方式來做到這一點 - 謝謝你的提示! – 2009-05-31 18:41:34
如果我想將其用於整個項目,該怎麼辦? – 2012-11-28 13:09:50
有人可以解釋爲什麼這隻適用於你明確定義類型(例如TextBox)?如果我使用FrameworkElement嘗試此操作,以便所有的孩子都有空間,那麼它就沒有任何作用。 – Schneider 2014-01-28 18:48:20