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.
Any ideas?
The Padding property represents the distance between an element and its child elements, and is used to separate the control from its own content. Padding values can be specified on layout classes.
Padding represents the distance between the side of the control (which can be the margin) and its content. The content depends on the type of the control. Margin is outside the UI element, while Padding is inside it.
StackPanel is a layout panel that arranges child elements into a single line that can be oriented horizontally or vertically. By default, StackPanel stacks items vertically from top to bottom in the order they are declared. You can set the Orientation property to Horizontal to stack items from left to right.
The margin adds extra space around the outside edges of an element. The Margin property of FrameworkElement represents the margin of an element. It is a type of Thickness structure. The Thickness structure has the four properties Left, Top, Right and Bottom.
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With