Question

While implementing IScrollInfo's MakeVisible member, I ran into an issue. I need to get the coordinates of that Visual's bounds relative to the panel which is being scrolled.

Now if this were a UIElement, this would be easy as I'd just call its 'TranslatePoint' method, but UIElement is a subclass of Visual, not the other way around, so I can't necessarily count on that.

How would one go about achieving this?

Was it helpful?

Solution

Visual provides the TransformToVisual method, which returns a GeneralTransform that can be used to transform points or rectangles:

var transform = visual1.TransformToVisual(visual2);
var point = transform.Transform(new Point(...));

If visual1 is a ContainerVisual, you can do this:

var bounds = transform.TransformBounds(visual1.ContentBounds);

or

var bounds = transform.TransformBounds(visual1.DescendantBounds);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top