Adding spaces between WPF controls

Shamim Hafiz picture Shamim Hafiz · May 15, 2011 · Viewed 37.6k times · Source

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.

Answer

honzakuzel1989 picture honzakuzel1989 · Oct 17, 2016

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>