Change the coordinate system of a Canvas in WPF

后端 未结 8 1784
一整个雨季
一整个雨季 2021-02-01 06:59

I\'m writing a mapping app that uses a Canvas for positioning elements. For each element I have to programatically convert element\'s Lat/Long to the canvas\' coordinate, then

8条回答
  •  梦谈多话
    2021-02-01 07:23

    I was able to get it to by creating my own custom canvas and overriding the ArrangeOverride function like so:

        public class CustomCanvas : Canvas
        {
            protected override Size ArrangeOverride(Size arrangeSize)
            {
                foreach (UIElement child in InternalChildren)
                {
                    double left = Canvas.GetLeft(child);
                    double top = Canvas.GetTop(child);
                    Point canvasPoint = ToCanvas(top, left);
                    child.Arrange(new Rect(canvasPoint, child.DesiredSize));
                }
                return arrangeSize;
            }
            Point ToCanvas(double lat, double lon)
            {
                double x = this.Width / 360;
                x *= (lon - -180);
                double y = this.Height / 180;
                y *= -(lat + -90);
                return new Point(x, y);
            }
        }
    

    Which works for my described problem, but it probably would not work for another need I have, which is a PathGeometry. It wouldn't work because the points are not defined as Top and Left, but as actual points.

提交回复
热议问题