Adding spaces between WPF controls

41,861

Solution 1

Add a Margin to your buttons

<Button Margin="10" x:Name="Button1" Width="100" Content="Button1" HorizontalAlignment="Left" VerticalAlignment="Top" ></Button>
<Button Margin="10"  x:Name="Button2" Width="100" Content="Button2" HorizontalAlignment="Left" VerticalAlignment="Top" ></Button>

The Margin will make sure there is at least that much space between each button and any other control

Something you might find useful is that you can have different margin values for top, left, right and bottom so:

Margin="10,0,10,0"

Would space the buttons out horizontally but wouldn't make them any smaller vertically...

Solution 2

If you do not use (for some reason) Button's Margin property, you can put transparent Separator (Transparent background color) with desired Width (or/and Height) between your controls (Buttons in your case).

In xaml:

<StackPanel Orientation="Horizontal">
  <Button x:Name="Button1" Width="100" Content="Button1"/>
  <Separator Width="20" Background="Transparent"/>
  <Button x:Name="Button2" Width="100" Content="Button2"/>
</StackPanel>

Solution 3

For me setting the foreground and background colors of a Separator to transparent did not work - it was still visible.

Instead I used the following:

<Separator Visibility="Hidden" Height="15"/>

I preferred this over setting a margin partly for the reasons cited in comments to another answer (it can have side-effects on the size of the item) and partly because I think using a separator is a little more clear for other programmers (or even myself) later on.

Share:
41,861

Related videos on Youtube

Shamim Hafiz - MSFT
Author by

Shamim Hafiz - MSFT

Help and get helped

Updated on July 09, 2022

Comments

  • Shamim Hafiz - MSFT
    Shamim Hafiz - MSFT almost 2 years

    I have created two buttons using the following XAML code.

    <Button x:Name="Button1" Width="100" Content="Button1" HorizontalAlignment="Left" VerticalAlignment="Top" ></Button>
                            <Button x:Name="Button2" Width="100" Content="Button2" HorizontalAlignment="Left" VerticalAlignment="Top" ></Button>
    

    The two buttons are tightly touching each other. How to put some space between them?

    Note: The buttons are located inside a stackpanel with Horizontal orientation.

  • Shamim Hafiz - MSFT
    Shamim Hafiz - MSFT about 13 years
    This isn't working for my case. Adding a margin shrinks the button in every dimension. Perhaps I should have added this before, the Button is inside a stackpanel.
  • Russell Troywest
    Russell Troywest about 13 years
    You could add Padding to the StackPanel - any controls within the stackpanel will then get padding placed around them. Will give a similar result.
  • Shamim Hafiz - MSFT
    Shamim Hafiz - MSFT about 13 years
    How to add padding. I don't seem to find any property by that name?
  • Russell Troywest
    Russell Troywest about 13 years
    See Here: danrigsby.com/blog/index.php/2008/05/20/… for details on how padding and margin work.
  • Russell Troywest
    Russell Troywest about 13 years
    Sorry, I was wrong about the padding I think. You will need to use Margin on your buttons to get the desired effect> Maybe you could post more of your XAML and we'll be able to give a solution where Margin would work for you
  • publicgk
    publicgk about 13 years
    @Gunner, isn't <Button x:Name="Button1" Width="100" Content="Button1" HorizontalAlignment="Left" VerticalAlignment="Top"></Button> <Button x:Name="Button2" Width="100" Content="Button2" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,0,0,0"></Button> working for you?
  • Shamim Hafiz - MSFT
    Shamim Hafiz - MSFT about 13 years
    @publicgk: Yes, Margin with all four parameters is now now working.
  • Roland Illig
    Roland Illig almost 8 years
    Your answer implicitly suggests that the order of the Margin properties is top, left, right, bottom, but it really is left, top, right, bottom.
  • Pancake5475
    Pancake5475 over 4 years
    @vargonian Setting Visibility to Hidden can also prevent you from clicking on the Separator in design view, which may or may not be desirable.
  • StayOnTarget
    StayOnTarget over 2 years
    For some reason this did not work for me... even when set to transparent, it was still visible (?)