Net Core WPF如何改变Canvas中的原点坐标

2021-11-05  乐帮网

wpf

Net Core WPF项目中使用元件Canvas,通过以下代码是无法修改原点坐标的。
.SetValue(Canvas.LeftProperty, 100.0);
.SetValue(Canvas.TopProperty, 100.0);
我们可以通过自定义元件来实现这个功能。以下是源码:

/// <summary>
    /// 自定义坐标系Canvas
    /// </summary>
    public class CoordinateCanvas: Canvas
    {

        /// <summary>
        /// 原点X坐标
        /// </summary>
        public double OriginX
        {
            get { return (double)GetValue(OriginXProperty); }
            set { SetValue(OriginXProperty, value); }
        }

        // Using a DependencyProperty as the backing store for OriginX.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OriginXProperty =
            DependencyProperty.Register("OriginX", typeof(double), typeof(CoordinateCanvas), new PropertyMetadata(0.0, OnOriginPropertyChanged));

        private static void OnOriginPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var uiElement = d as UIElement;
            if (uiElement != null)
            {
                uiElement.InvalidateArrange();
            }
        }

        /// <summary>
        /// 原点Y坐标
        /// </summary>
        public double OriginY
        {
            get { return (double)GetValue(OriginYProperty); }
            set { SetValue(OriginYProperty, value); }
        }

        // Using a DependencyProperty as the backing store for OriginY.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OriginYProperty =
            DependencyProperty.Register("OriginY", typeof(double), typeof(CoordinateCanvas), new PropertyMetadata(0.0, OnOriginPropertyChanged));

        protected override Size ArrangeOverride(Size arrangeSize)
        {
            Point originalPoint = new Point();
            if (OriginX > 0 && OriginX <= arrangeSize.Width)
                originalPoint.X = OriginX;
            if (OriginY > 0 && OriginY <= arrangeSize.Height)
                originalPoint.Y = OriginY;
            foreach (UIElement element in InternalChildren)
            {
                if (element == null) continue;
                double x = 0.0;
                double y = 0.0;
                double left = GetLeft(element);
                if (!double.IsNaN(left))
                {
                    x = left;
                }
                double top = GetTop(element);
                if (!double.IsNaN(top))
                {
                    y = top;
                }
                element.Arrange(new Rect(new Point(originalPoint.X + x, originalPoint.Y + y), element.DesiredSize));
            }
            return arrangeSize;
        }

    }

前端的xaml如下:

 <local:CoordinateCanvas x:Name="Canvas" OriginX="100" OriginY="100">
            <Ellipse Canvas.Left="0" Canvas.Top="0" Fill="Red" Width="100" Height="100"/>
        </local:CoordinateCanvas>

效果图如下:

canvas

 

源码下载:链接:https://pan.baidu.com/s/10-UIDg1VqOxrf3u1-wKgWg 

 

公众号二维码

关注我的微信公众号
在公众号里留言交流
投稿邮箱:1052839972@qq.com

庭院深深深几许?杨柳堆烟,帘幕无重数。
玉勒雕鞍游冶处,楼高不见章台路。
雨横风狂三月暮。门掩黄昏,无计留春住。
泪眼问花花不语,乱红飞过秋千去。

欧阳修

付款二维码

如果感觉对您有帮助
欢迎向作者提供捐赠
这将是创作的最大动力