在 WPF 控件之间添加空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6008001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:51:37  来源:igfitidea点击:

Adding spaces between WPF controls

wpfxamlbuttonspacing

提问by Shamim Hafiz

I have created two buttons using the following XAMLcode.

我使用以下XAML代码创建了两个按钮。

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

注意:按钮位于水平方向的堆栈面板内。

回答by Russell Troywest

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

将按钮水平间隔开,但不会使它们垂直变小......

回答by honzakuzel1989

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

如果您不使用(出于某种原因)Button 的 Margin 属性,您可以在您的控件(在您的情况下为按钮)之间放置具有所需宽度(或/和高度)的透明分隔符(透明背景颜色)。

In xaml:

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