Note that there are some explanatory texts on larger screens.

plurals
  1. POTranslatePoint within a Canvas
    primarykey
    data
    text
    <p>I have a scroll viewer in my application that contains a canvas, in which I have a tree custom-drawn tree structure. I'm trying to get the position of a particular node element in the canvas relative to the scroll viewer (so I can scroll to it), but my attempts aren't working.</p> <p>I've tried using <code>marker.TranslatePoint(new Point(0, 0), scrollViewer)</code> (where <code>marker</code> is the element in the canvas), but this is just returning the position of the canvas, rather than the marker. Similarly, if I try <code>marker.TranslatePoint(new Point(0, 0), layoutCanvas)</code>, I invariably just get <code>(0,0)</code> as the result, regardless of where the marker actually is.</p> <p>Here's my code:</p> <pre><code>var marker = m_Metadata[node].Marker; var location = marker.TranslatePoint(new Point(0, 0), scrollViewer); // This inorrectly gives the position of the canvas, rather than the marker. var size = new Size(marker.Width, marker.Height); var markerArea = new Rect(location, size); double horizontalOffset = (markerArea.Right + markerArea.Left - scrollViewer.ViewportWidth) / 2; double verticalOffset = (markerArea.Bottom + markerArea.Top - scrollViewer.ViewportHeight) / 2; </code></pre> <p>I've also tried using <code>marker.TransformToVisual(scrollViewer).Transform(new Point(0, 0)</code>, but this gives the same results.</p> <p>I can work around it by using <code>Canvas.GetLeft</code>, <code>Canvas.GetTop</code> et al, but this is messy and convoluted (since they won't always be left- and top-aligned).</p> <p>How can I fix this, or does it just not work for canvases?</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.
 

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