如何在底部/右侧正确放置按钮?

时间:2016-08-08 15:02:24

标签: wpf

我尝试从右下角放置一个10px的按钮。在Designer中,按钮距离角落约10px,但不在程序中(距离角落只有1px,看起来有点不好)。这是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>

this is how it looks in the program this is how it looks in the Designer

3 个答案:

答案 0 :(得分:3)

您正在根据保证金设置按钮。尝试将按钮更改为下方。

<Button x:Name="button" Content="Button" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="75" Margin="10"/>

答案 1 :(得分:3)

如果您打算使用WPF或UWP,您肯定希望能够理解xaml标记。根据我的经验,拖放设计师很少能提供你真正想要的东西。但是,设计师和属性会准确地告诉您发生了什么。您是否看到如果在运行时最大化窗口会发生什么?按钮将无法靠近底部右下角。根据定义,它将始终是左侧207px和顶部239px。请参阅下面的快速说明。

  • 蓝色 - 对象与此边缘相距一定距离。
  • 红色 - 约束对象的值。
  • 绿色 - 对象不受此边缘限制。

enter image description here

如果要求按钮的右边缘为10px,底部为10px,则可以通过单击&#34;链接&#34;来定义按钮。为了使预期的边缘成为参考边缘的约束(点击它们切换它们,所以&#34;关闭&#34;左边和顶部),并调整值。您可能想要摆脱设计师为您提供的变换。

enter image description here

答案 2 :(得分:0)

使用边距时,始终首选最近边。 这里的“右侧”为Horizo​​ntalAlignment,“Bottom Side”为VerticalAlignment为最近。

相关问题