Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding spaces between WPF controls

Tags:

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.

like image 910
Shamim Hafiz - MSFT Avatar asked May 15 '11 11:05

Shamim Hafiz - MSFT


People also ask

How do you add a space between two controls in WPF?

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).

How do I give space in XAML?

There are several techniques for preserving white space in the source XAML for eventual presentation that are not affected by XAML processor white-space normalization. xml:space="preserve": Specify this attribute at the level of the element where white-space preservation is desired.

What is padding in WPF?

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.

What is a viewbox in WPF?

The Viewbox control is used to stretch or scale a child element.


2 Answers

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> 
like image 156
honzakuzel1989 Avatar answered Sep 22 '22 16:09

honzakuzel1989


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...

like image 23
Russell Troywest Avatar answered Sep 21 '22 16:09

Russell Troywest