开发者

Adding spaces between WPF controls

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:Na开发者_JAVA技巧me="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.


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>


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜