我有一个带有基本自定义FrameworkElement实现的简单测试应用(下面的TestElement)。 TestElement创建了一些绘图视觉效果,并在构造函数中绘制了一些东西,使其宽度为600。它还实现了IScrollinfo的必要位;包含元素的窗口具有滚动查看器,最大尺寸为300x300。出现滚动条,但不滚动TestElement的内容。

谁能建议我尝试做的事情是否可能,如果可以,我做错了什么。我可以在SetHorizo​​ntalOffset中重新渲染图形视觉效果,但由于性能原因,我不想这样做,因为我已经绘制了所有需要的图形。

我希望这个问题有意义-让我知道是否可以澄清。

非常感谢-卡尔

public class TestElement : FrameworkElement, IScrollInfo
{
    DrawingVisual visual;
    DrawingVisual visual2;
    public TestElement()
    {
        Draw();
        this.MaxWidth = 600;
        this.MaxHeight = 300;
    }
    public void Draw()
    {
        if(visual == null)
        {
            visual = new DrawingVisual();
            base.AddVisualChild(visual);
            base.AddLogicalChild(visual);
        }
        if (visual2 == null)
        {
            visual2 = new DrawingVisual();
            base.AddVisualChild(visual2);
            base.AddLogicalChild(visual2);
        }
        Random rand = new Random();
        var pen = new Pen(Brushes.Black, 1);
        using(var dc = visual.RenderOpen())
        {
            for (int i = 0; i < 400; i++)
            {
                var r = rand.Next(10, 200);
                dc.DrawLine(pen, new Point(i, r), new Point(i, 0));
            }
        }
        using (var dc = visual2.RenderOpen())
        {
            for (int i = 0; i < 200; i++)
            {
                var r = rand.Next(10, 200);
                dc.DrawLine(pen, new Point(i, r), new Point(i, 0));
            }
            visual2.Offset = new Vector(400, 0);
        }
    }
    protected override int VisualChildrenCount
    {
        get { return 2; }
    }
    protected override Visual GetVisualChild(int index)
    {
        return index == 0 ? visual : visual2;
    }
    protected override Size MeasureOverride(Size availableSize)
    {
        viewport = availableSize;
        owner.InvalidateScrollInfo();
        return base.MeasureOverride(availableSize);
    }
    protected override Size ArrangeOverride(Size finalSize)
    {
        var value = base.ArrangeOverride(finalSize);
        return base.ArrangeOverride(finalSize);
    }


    Point offset = new Point(0,0);
    public void SetHorizontalOffset(double offset)
    {
        this.offset.X = offset;
        this.InvalidateArrange();
    }

    public void SetVerticalOffset(double offset)
    {
        this.offset.Y = offset;
    }

    public Rect MakeVisible(Visual visual, Rect rectangle)
    {
        throw new NotImplementedException();
    }

    public bool CanVerticallyScroll { get; set; }
    public bool CanHorizontallyScroll { get; set; }

    Size extent = new Size(600, 300);

    private Size viewport = new Size(0, 0);
    public double ExtentWidth
    {
        get { return extent.Width; }
    }

    public double ExtentHeight
    {
        get {return extent.Height; }
    }

    public double ViewportWidth
    {
        get { return viewport.Width; }
    }

    public double ViewportHeight
    {
        get { return viewport.Height; }
    }

    public double HorizontalOffset
    {
        get { return offset.X; }
    }

    public double VerticalOffset
    {
        get { return offset.Y; }
    }

    private ScrollViewer owner;

    public ScrollViewer ScrollOwner
    {
        get { return owner; }
        set { owner = value; }
    }


}


xaml:

<Window x:Class="TestWpfApp.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l ="clr-namespace:TestWpfApp"
Title="TestWpfApp" Height="300" Width="300" >
<Grid>
    <ScrollViewer CanContentScroll="True" HorizontalScrollBarVisibility="Visible">
        <l:TestElement CanHorizontallyScroll="True" />
    </ScrollViewer>
</Grid>

最佳答案

刚刚也喝了啤酒,这有助于找到解决方案。 ;-)

这不是最终的“一切都很好”的解决方案,但是可以肯定的是它将进一步帮助您:

ArrangeOverride的实现中,尝试:

protected override Size ArrangeOverride(Size finalSize)
{
    this.visual.Offset = new Vector(-this.HorizontalOffset, -this.VerticalOffset);

    var value = base.ArrangeOverride(finalSize);
    return base.ArrangeOverride(finalSize);
}


基本上,您必须自己移动对象。

有关更多信息,请参见本文:iscrollinfo tutorial

通常,您必须使用转换将对象移动到滚动对象的位置。

08-25 08:59