wpf 如何在底部/右侧正确放置按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38832585/
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
How to place a button correctly on the bottom/right?
提问by Dennis R
I try to place a button 10px from the right and bottom corner. In the Designer the button is about 10px from the corner, but not in the program (it is only 1px from the corner which looks a bit bad). Is this a bug in WPF?
我尝试在距离右下角 10 像素处放置一个按钮。在设计器中,按钮距角约 10 像素,但在程序中不是(距角仅 1 像素,看起来有点糟糕)。这是 WPF 中的错误吗?
<Window x:Class="wpftest.test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:wpftest" mc:Ignorable="d" Title="test"
Height="300" Width="300">
<Grid>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="207,239,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.133,-0.75"/>
</Grid>
</Window>
回答by Mark W
You will definitely want to be able to understand xaml markup if you intend on using WPF or UWP. Drag and drop in the designer rarely gives you what you really want, in my experience. However, the designer and the properties tell you exactly what is going on. Did you see what happens if you maximize your window at runtime? The button will be nowhere near the bottom, right corner. It will always be 207px from the left and 239px from the top, as defined. See below for a quick explanation.
如果您打算使用 WPF 或 UWP,您肯定希望能够理解 xaml 标记。根据我的经验,在设计器中拖放很少会提供您真正想要的东西。但是,设计器和属性会准确地告诉您发生了什么。您是否看到如果在运行时最大化窗口会发生什么?该按钮将不会靠近右下角。根据定义,它始终距左侧 207 像素,距顶部 239 像素。请参阅下面的快速解释。
- Blue - the object is constrained a distance from this edge.
- Red - the value by which the object is constrained.
- Green - the object is not constrained to this edge.
- 蓝色 - 对象被限制在距此边缘一段距离处。
- 红色 - 对象受约束的值。
- 绿色 - 对象不受此边缘的约束。
If you requirement be that the button be 10px from the right edge and 10px from the bottom, you can define that by clicking the "links" for the constraints to make the intended edges the reference edges (clicking them toggles them, so "turn off" the left and top), and adjust the values. You'll probably want to get rid of that transform that the designer put in for you as well.
如果您要求按钮距右边缘 10 像素,距底部 10 像素,您可以通过单击约束的“链接”来定义,以使预期边缘成为参考边缘(单击它们会切换它们,因此“关闭" 左侧和顶部),并调整值。您可能还想摆脱设计师为您设置的转换。
回答by AVK
You are setting your button based on Margin. Try Changing your button to below.
您正在根据保证金设置按钮。尝试将您的按钮更改为下方。
<Button x:Name="button" Content="Button" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="75" Margin="10"/>
回答by maulik kansara
when working with the margins, always preferred nearest side. here for your "right side" as HorizontalAlignment and "Bottom Side" as VerticalAlignment as nearest.
在处理边距时,总是首选最近的一侧。此处将您的“右侧”设置为 HorizontalAlignment,将“底部”设置为 VerticalAlignment 作为最近的位置。


