在 WPF 控件之间添加空格
我使用以下 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>
两个按钮紧紧地接触在一起。如何在它们之间留出一些空间?
注意: 按钮位于水平方向的堆栈面板内。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不使用(由于某种原因)按钮的 Margin 属性,您可以在控件(您的情况下为按钮)之间放置具有所需宽度(或/和高度)的透明分隔符(透明背景色)。
在 xaml 中:
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:
为按钮添加边距 边距
将确保每个按钮和任何其他控件之间至少有足够的空间,
可能会发现有用的是,您可以为顶部、左侧、右侧和底部设置不同的边距值,因此:
您 按钮水平伸出,但垂直方向不会变小......
Add a Margin to your buttons
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:
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:
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.