Search code examples
wpfbuttonalignmentstackpanel

How to add several button in stack panel with spaces between buttons


I try to put several buttons in StackPanel:

<StackPanel Orientation="Horizontal">
    <Button Content="" HorizontalAlignment="Left" Margin="10,590,1,-40"
        VerticalAlignment="Top" RenderTransformOrigin="1.25,0.964">
    </Button>
    <Button Content="Add" HorizontalAlignment="Left" Margin="10,590,10,-40" 
        VerticalAlignment="Top" RenderTransformOrigin="1.25,0.964">
    </Button>
</StackPanel>

All my Buttons alignment to the right side which is OK but with no spaces between them.

Any suggestions how to do that ?


Solution

  • Use the Margin property....

    Default is Left, Top, Right, Bottom, so in your sample code you have Left = 10, Top = 590, Right = 1, and Bottom = -40.

    Change the Left/Right Margins to something reasonable. Here's some (cleaned up) example code :

    <StackPanel Orientation="Horizontal">
        <Button Content="" Margin="0,0,10,0" VerticalAlignment="Top" />
        <Button Content="Add" Margin="0,0,10,0" VerticalAlignment="Top" />
    </StackPanel>