这是装饰器BUG吗?

发布于 2024-11-06 20:01:59 字数 2752 浏览 3 评论 0原文

我在使用装饰器时遇到了一个奇怪的问题。通过使用继承自装饰器的类,我尝试在 ScrollViewer 中动态绘制一条线。当线短于特定长度(大约 600)时,它效果很好,但是如果我尝试滚动ScrollViewer并绘制更长的线,问题来了。线似乎被剪断了,其余超过600的部分似乎看不见了。我检查了线路的X1,X2,Y1,Y2,似乎是正确的。您能给一些建议吗?非常感谢!

这是我的装饰器类

 internal class LinkGanttTaskAdorner : Adorner
{
    private readonly Line child = null;
    private double x2 = 0;
    private double y2 = 0;
    private double x1 = 0;
    private double y1 = 0;
    public LinkGanttTaskAdorner(UIElement adornedElement)
        : base(adornedElement)
    {
        var brush = new VisualBrush(adornedElement);
        child = new Line
        {
            Stroke = Brushes.Black,
            StrokeThickness = 1
        };
    }


    protected override Size MeasureOverride(Size constraint)
    {
        child.Measure(constraint);
        return child.DesiredSize;
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        child.Arrange(new Rect(finalSize));
        return finalSize;
    }

    protected override Visual GetVisualChild(int index)
    {
        return child;
    }

    protected override int VisualChildrenCount
    {
        get
        {
            return 1;
        }
    }

    public double X1
    {
        get
        {
            return x1;
        }
        set
        {
            x1 = value;
            child.X1 = value;
        }
    }

    public double Y1
    {
        get
        {
            return y1;
        }
        set
        {
            y1 = value;
            child.Y1 = value;
        }
    }

    public double X2
    {
        get
        {
            return x2;
        }
        set
        {
            x2 = value;
            child.X2 = value;
            UpdatePosition();
        }
    }

    public double Y2
    {
        get
        {
            return y2;
        }
        set
        {
            y2 = value;
            child.Y2 = value;
            UpdatePosition();

        }
    }

    private void UpdatePosition()
    {
        var adornerLayer = this.Parent as AdornerLayer;
        if (adornerLayer != null)
        {
            adornerLayer.Update(AdornedElement);
        }
    }

通过使用 MouseMove 事件来绘制线条

   private void DragLinkStarted()
    {
        isMoveTask = false;
        isLinkTask = true;

        linkAdorner = new LinkGanttTaskAdorner(originalElement) { X1 = startMousePoint.X, Y1 = startMousePoint.Y };
        var layer = AdornerLayer.GetAdornerLayer(originalElement);
        layer.Add(linkAdorner);
        Cursor = Cursors.Cross;
    }

   private void DragLinkMoved()
    {
        mousePoint = Mouse.GetPosition(originalElement);
        linkAdorner.X2 = mousePoint.X;
        linkAdorner.Y2 = mousePoint.Y;
    }

I've encountered a strange problem working with adorners.By using a class inheriting from Adorner,I try to draw a line in a ScrollViewer dynamically.It works well when the line is shorter than a particular length(about 600),but if I tried to scroll the ScrollViewer and draw the line longer,the problem came.The line appeared to be clipped and the rest part which is longer than 600 seemed to be invisible. I had checked the line's X1,X2,Y1,Y2,it seemed to be correct.Could you please give some advices?Thanks a lot!

Here is my Adorner Class

 internal class LinkGanttTaskAdorner : Adorner
{
    private readonly Line child = null;
    private double x2 = 0;
    private double y2 = 0;
    private double x1 = 0;
    private double y1 = 0;
    public LinkGanttTaskAdorner(UIElement adornedElement)
        : base(adornedElement)
    {
        var brush = new VisualBrush(adornedElement);
        child = new Line
        {
            Stroke = Brushes.Black,
            StrokeThickness = 1
        };
    }


    protected override Size MeasureOverride(Size constraint)
    {
        child.Measure(constraint);
        return child.DesiredSize;
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        child.Arrange(new Rect(finalSize));
        return finalSize;
    }

    protected override Visual GetVisualChild(int index)
    {
        return child;
    }

    protected override int VisualChildrenCount
    {
        get
        {
            return 1;
        }
    }

    public double X1
    {
        get
        {
            return x1;
        }
        set
        {
            x1 = value;
            child.X1 = value;
        }
    }

    public double Y1
    {
        get
        {
            return y1;
        }
        set
        {
            y1 = value;
            child.Y1 = value;
        }
    }

    public double X2
    {
        get
        {
            return x2;
        }
        set
        {
            x2 = value;
            child.X2 = value;
            UpdatePosition();
        }
    }

    public double Y2
    {
        get
        {
            return y2;
        }
        set
        {
            y2 = value;
            child.Y2 = value;
            UpdatePosition();

        }
    }

    private void UpdatePosition()
    {
        var adornerLayer = this.Parent as AdornerLayer;
        if (adornerLayer != null)
        {
            adornerLayer.Update(AdornedElement);
        }
    }

By using the MouseMove event to Draw the line

   private void DragLinkStarted()
    {
        isMoveTask = false;
        isLinkTask = true;

        linkAdorner = new LinkGanttTaskAdorner(originalElement) { X1 = startMousePoint.X, Y1 = startMousePoint.Y };
        var layer = AdornerLayer.GetAdornerLayer(originalElement);
        layer.Add(linkAdorner);
        Cursor = Cursors.Cross;
    }

   private void DragLinkMoved()
    {
        mousePoint = Mouse.GetPosition(originalElement);
        linkAdorner.X2 = mousePoint.X;
        linkAdorner.Y2 = mousePoint.Y;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文