Padding on StackPanel?

WpfXamlLayoutPaddingStackpanel

Wpf Problem Overview


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?

Wpf Solutions


Solution 1 - Wpf

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.

Solution 2 - Wpf

Or you could do something similar to TiM:

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

Solution 3 - Wpf

if you mean you want to space out child elements of the StackPanel (which doesn't have a Padding property), see: https://stackoverflow.com/questions/932510/how-do-i-space-out-the-child-elements-of-a-stackpanel

Solution 4 - Wpf

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.

Solution 5 - Wpf

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShimmy WeitzhandlerView Question on Stackoverflow
Solution 1 - WpfTim SwastView Answer on Stackoverflow
Solution 2 - WpfOliverView Answer on Stackoverflow
Solution 3 - WpfGeorge BirbilisView Answer on Stackoverflow
Solution 4 - WpfRobView Answer on Stackoverflow
Solution 5 - WpfMicroshineView Answer on Stackoverflow