Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Custom FrameworkElement / IScrollInfo
    primarykey
    data
    text
    <p>I've got a simple test app with a basic custom <code>FrameworkElement</code> 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. </p> <p>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.</p> <p>I hope the question makes some sense - let me know if not and I can clarify.</p> <p>Many thanks - Karl</p> <pre><code>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 &lt; 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 &lt; 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; } } } </code></pre> <p>the xaml:</p> <pre><code>&lt;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" &gt; &lt;Grid&gt; &lt;ScrollViewer CanContentScroll="True" HorizontalScrollBarVisibility="Visible"&gt; &lt;l:TestElement CanHorizontallyScroll="True" /&gt; &lt;/ScrollViewer&gt; &lt;/Grid&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload