WPF Custom FrameworkElement / IScrollInfo

4.4k views Asked by At

I've got a simple test app with a basic custom FrameworkElement implementation (TestElement below). The TestElement creates a couple of drawing visuals and draws some stuff in the constructor to a width of 600. It also implements the necessary bits of IScrollinfo; The Window containing the element has got a scrollviewer and a max size of 300x300. The scrollbar appears but does not scroll the content of the TestElement.

Can anyone suggest whether what I am trying to do is possible and if so what I am doing wrong. I could re-render the drawing visuals in SetHorizontalOffset but don't want to for performance reasons as I have already drawn all I need to.

I hope the question makes some sense - let me know if not and I can clarify.

Many thanks - Karl

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


}

the 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>

1

There are 1 answers

1
fixagon On BEST ANSWER

Just had a beer too and it helps to find a solution. ;-)

It's not the ultimative "all well done" solution, but for sure it will help you further:

In your implementation of ArrangeOverride, try:

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

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

Basically you have to move your objects yourself.

For more information see this article too: iscrollinfo tutorial . Normally you would have to use Transformations to move the objects there where you scrolled them.