Mark A. Donohoe
Mark A. Donohoe

Reputation: 30408

How can I get the bounds of a Visual relative to another visual's coordinate system?

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?

Upvotes: 0

Views: 253

Answers (1)

Clemens
Clemens

Reputation: 128070

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

Upvotes: 1

Related Questions