wpf 在 StackPanel 上填充?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1303693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Padding on StackPanel?
提问by Shimmy Weitzhandler
I am trying to set padding of a StackPanel but there ain't such property. I tried StackPanel.Border, but there is no such property either.
我正在尝试设置 StackPanel 的填充,但没有这样的属性。我试过 StackPanel.Border,但也没有这样的属性。
Any ideas?
有任何想法吗?
回答by Tim Swast
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.
您可以在 StackPanel 周围放置一个 Border 并在其上设置一个填充。我最终做了很多这样的事情,因为有很多 UIElements 没有 padding 属性。
<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).
(注意:所有 FrameworkElements 都有一个 Margin 属性,它将分隔元素,但不包括作为 ActualWidth 一部分的边距宽度)。
If you want to space the items inside a StackPanel, you'll want to add a margin to each child as Rob said.
如果您想将 StackPanel 内的项目隔开,您需要像 Rob 所说的那样为每个孩子添加一个边距。
回答by Oliver
Or you could do something similar to TiM:
或者你可以做一些类似于 TiM 的事情:
<Border>
<StackPanel Margin="10">
...
</StackPanel>
</Border>
回答by George Birbilis
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?
如果您的意思是要隔开 StackPanel 的子元素(它没有 Padding 属性),请参阅:如何隔开 StackPanel 的子元素?
回答by Microshine
This is a variant of padding for elements of StackPanel
这是 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>
回答by Rob
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.
您可能希望为面板中的项目添加边距。您将得到相同的结果,只需向后接近即可。
回答by benPearce
Could it be Margin you are looking for?
可能是您要寻找的保证金吗?