Net Core WPF 布局中使用Margin调整位置,它是以以默认基准点计算偏移的。如果此时我们使用<Grid>进行布局像下面这样。

 <Grid>
        <Button Margin="150,60,0,0"  Width="90" Height="30"  Name="btn">button</Button>
    </Grid>

就会发现它是以屏幕中心为基准向左向下分别偏移150和60。
如果我们想要以左上角为基准点偏移呢?应该怎么设置?答案如下:可以在按钮上添加VerticalAlignment 和 HorizontalAlignment属性。

调整后代码如下:

  <Grid>
        <Button Margin="150,60,0,0"  Width="90" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left" Name="btn">button</Button>
    </Grid>

效果图如下:

cide