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.
I've tried using marker.TranslatePoint(new Point(0, 0), scrollViewer)
(where marker
is the element in the canvas), but this is just returning the position of the canvas, rather than the marker. Similarly, if I try marker.TranslatePoint(new Point(0, 0), layoutCanvas)
, I invariably just get (0,0)
as the result, regardless of where the marker actually is.
Here's my 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;
I've also tried using marker.TransformToVisual(scrollViewer).Transform(new Point(0, 0)
, but this gives the same results.
I can work around it by using Canvas.GetLeft
, Canvas.GetTop
et al, but this is messy and convoluted (since they won't always be left- and top-aligned).
How can I fix this, or does it just not work for canvases?