这是一个Adorner BUG吗?

时间:2011-05-12 03:10:37

标签: wpf adorner

我在使用adorner时遇到了一个奇怪的问题。通过使用继承自Adorner的类,我尝试动态地在ScrollViewer中绘制一条线。当线条比特定长度(大约600)短时效果很好,但是如果我试图滚动ScrollViewer并画出更长的线条,问题就出现了。线条似乎被修剪了,其余部分超过600似乎是看不见的。我检查了线路的X1,X2,Y1,Y2,它似乎是正确的。请你提出一些建议吗?非常感谢!

这是我的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);
        }
    }

使用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;
    }

0 个答案:

没有答案