【问题标题】:How to change x, y co-ordinates of canvas to bottom-right corner in UWP?如何在 UWP 中将画布的 x、y 坐标更改为右下角?
【发布时间】:2022-01-12 11:07:30
【问题描述】:

UWP 画布的坐标系从控件左上角的 (0,0) 开始。如何更改坐标以便 (0, 0) 出现在画布的右下角?

【问题讨论】:

    标签: c# xaml uwp


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2010-11-14
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多