diff --git a/src/Avalonia.Visuals/VisualExtensions.cs b/src/Avalonia.Visuals/VisualExtensions.cs index 226e616369..d69785cc59 100644 --- a/src/Avalonia.Visuals/VisualExtensions.cs +++ b/src/Avalonia.Visuals/VisualExtensions.cs @@ -45,6 +45,22 @@ namespace Avalonia return p.Item1.PointToScreen(point + p.Item2); } + /// + /// Translates a point relative to this visual to coordinates that are relative to the specified visual. + /// The visual and relativeTo should be descendants of the same root window + /// + /// The visual. + /// The point value, as relative to this visual. + /// The visual to translate the given point into. + /// A point value, now relative to the target visual rather than this source element. + public static Point TranslatePoint(this IVisual visual, Point point, IVisual relativeTo) + { + var pos = GetRootAndPosition(visual); + var relToPos = GetRootAndPosition(relativeTo); + + return point - (relToPos.Item2 - pos.Item2); + } + /// /// Gets the root of the control's visual tree and the position of the control /// in the root's coordinate space.