如何将 canvas 的 x, y 坐标更改为 UWP 中的右下角?

How to change x, y co-ordinates of canvas to bottom-right corner in UWP?

UWP Canvas 的坐标系从控件左上角的 (0,0) 开始。如何更改坐标,使 (0, 0) 出现在 canvas?

的右下角

(0, 0) 总是指 Canvas.

的左上角

如果想让子控件出现在右下角,需要根据Canvas的大小和控件本身的大小计算坐标:

Button child = new Button { Content = "Click me!", Height = 33, Width = 78};
Canvas.SetTop(child, canvas.Height - child.Height);
Canvas.SetLeft(child, canvas.Width - child.Width);
canvas.Children.Add(child);

XAML:

 <Canvas x:Name="canvas" Width="200" Height="200" Background="Yellow" />

如果您没有明确设置控件的大小,您可以等到它被渲染后才能获得它的大小:

Button child = new Button { Content = "Click me!" };
canvas.Children.Add(child);
child.Loaded += (s, e) =>
{
    Canvas.SetTop(child, canvas.Height - child.ActualHeight);
    Canvas.SetLeft(child, canvas.Width - child.ActualWidth);
};