Ray*_*rns 20
这很简单:
public static Rect BoundsRelativeTo(this FrameworkElement element,
Visual relativeTo)
{
return
element.TransformToVisual(relativeTo)
.TransformBounds(LayoutInformation.GetLayoutSlot(element));
}
Run Code Online (Sandbox Code Playgroud)
事实上,将它放在一个单独的方法中可能有点过分.
LayoutSlot选项根本不适合我。最终给了我一个相对于指定父/祖先控件的子位置:
public static Rect BoundsRelativeTo(this FrameworkElement child, Visual parent)
{
GeneralTransform gt = child.TransformToAncestor(parent);
return gt.TransformBounds(new Rect(0, 0, child.ActualWidth, child.ActualHeight));
}
Run Code Online (Sandbox Code Playgroud)
考虑到我在这里找到的一些建议,这为我解决了问题。
item.TransformToVisual( relativeToElement )
.TransformBounds( new Rect( item.RenderSize ) );
Run Code Online (Sandbox Code Playgroud)