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

Time:12-10

The UWP Canvas has a coordinate system starting at (0,0) at the top-left of the control. How do I change the co-ordinates so that (0, 0) appears at bottom-right corner of the canvas?

CodePudding user response:

(0, 0) always refers to the top-left corner of the Canvas.

If you want the child control to appear at the bottom-right corner, you should calculate the coordinates based on the size of the Canvas and the control itself:

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" />

If you don't set the size of the control explicitly, you could wait until it has been rendered to get its size:

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);
};

enter image description here

  • Related