在堆栈面板上填充? ?

我试图设置一个 StackPanel 的填充,但是没有这样的属性。 我试过 StackPanel,边境,但也没有这样的财产。

有什么想法吗?

54855 次浏览

You'll probably want to add margin to the items in the panel instead. You'll get the same result, just have to approach it backward.

You could put a Border around the StackPanel and set a padding on that. I end up doing this a lot, since there are many UIElements that do not have a padding property.

<Border Padding="10">
<StackPanel>
<!--...-->
</StackPanel>
</Border>

(Note: all FrameworkElements have a Margin property, which will space the element, but not include the margin width as part of the ActualWidth).

If you want to space the items inside a StackPanel, you'll want to add a margin to each child as Rob said.

Or you could do something similar to TiM:

<Border>
<StackPanel Margin="10">
...
</StackPanel>
</Border>

if you mean you want to space out child elements of the StackPanel (which doesn't have a Padding property), see: How do I space out the child elements of a StackPanel?

This is a variant of padding for elements of StackPanel

<StackPanel Orientation="Horizontal" Grid.Row="2">
<StackPanel.Resources>
<Style x:Key="StackPanelPadding" TargetType="Control">
<Setter Property="Margin" Value="5,0,0,0"/>
<Setter Property="Height" Value="40"/>
</Style>
<Style BasedOn="{StaticResource StackPanelPadding}" TargetType="Button"/>
</StackPanel.Resources>
<Button/>
<Button/>
</StackPanel>