确定WPF元素相对于某个父级的边界矩形

时间:2021-11-07 23:02:21

I consider this a pretty simple request, but I can't seem to find a conclusive answer in my searches. How can I determine the bounds of a particular visual element in my window, relative to some other parent element?

我认为这是一个非常简单的请求,但我似乎无法在搜索中找到确凿的答案。如何确定窗口中特定视觉元素相对于其他父元素的边界?

I've tried using LayoutInformation.GetLayoutSlot but this just seems to return a Rect at 0,0 and doesn't reflect the actual location of the element.

我尝试过使用LayoutInformation.GetLayoutSlot,但这似乎只是在0,0处返回一个Rect,并不反映元素的实际位置。

What I'm trying to do is take a "screenshot" of a window using RenderTargetBitmap and then crop it to a particular element, but I can't get the element's bounds to know what to crop the bitmap to!

我想要做的是使用RenderTargetBitmap拍摄窗口的“屏幕截图”,然后将其裁剪为特定元素,但我无法获得元素的界限以知道要将位图裁剪为什么!

4 个解决方案

#1


16  

It is quite simple:

这很简单:

public static Rect BoundsRelativeTo(this FrameworkElement element,
                                         Visual relativeTo)
{
  return
    element.TransformToVisual(relativeTo)
           .TransformBounds(LayoutInformation.GetLayoutSlot(element));
}

In fact it may be overkill to put it in a separate method.

事实上,将它放在一个单独的方法中可能有点过分。

#2


4  

The LayoutSlot option didn't work for me at all. This ended up giving me a child position relative to a specified parent/ancestor control:

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

#3


3  

Nevermind, I finally managed to figure it out using a combination of LayoutInformation.GetLayoutSlot() (although I probably could have used either ActualWidth/ActualHeight or RenderSize) and UIElement.TranslatePoint().

没关系,我终于设法使用LayoutInformation.GetLayoutSlot()(虽然我可能使用ActualWidth / ActualHeight或RenderSize)和UIElement.TranslatePoint()的组合来解决它。

Seems a rather complicated solution when it could be as simple as this:

当它可以像这样简单时,似乎是一个相当复杂的解决方案:

myElement.GetBounds( relativeElement );

Oh well. Maybe time for an extension method. :)

好吧。也许有时间进行扩展方法。 :)

#4


0  

Taking into account a few suggestions i found here this solved the problem for me.

考虑到我在这里找到的一些建议,这解决了我的问题。

item.TransformToVisual( relativeToElement )
    .TransformBounds( new Rect( item.RenderSize ) );

#1


16  

It is quite simple:

这很简单:

public static Rect BoundsRelativeTo(this FrameworkElement element,
                                         Visual relativeTo)
{
  return
    element.TransformToVisual(relativeTo)
           .TransformBounds(LayoutInformation.GetLayoutSlot(element));
}

In fact it may be overkill to put it in a separate method.

事实上,将它放在一个单独的方法中可能有点过分。

#2


4  

The LayoutSlot option didn't work for me at all. This ended up giving me a child position relative to a specified parent/ancestor control:

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

#3


3  

Nevermind, I finally managed to figure it out using a combination of LayoutInformation.GetLayoutSlot() (although I probably could have used either ActualWidth/ActualHeight or RenderSize) and UIElement.TranslatePoint().

没关系,我终于设法使用LayoutInformation.GetLayoutSlot()(虽然我可能使用ActualWidth / ActualHeight或RenderSize)和UIElement.TranslatePoint()的组合来解决它。

Seems a rather complicated solution when it could be as simple as this:

当它可以像这样简单时,似乎是一个相当复杂的解决方案:

myElement.GetBounds( relativeElement );

Oh well. Maybe time for an extension method. :)

好吧。也许有时间进行扩展方法。 :)

#4


0  

Taking into account a few suggestions i found here this solved the problem for me.

考虑到我在这里找到的一些建议,这解决了我的问题。

item.TransformToVisual( relativeToElement )
    .TransformBounds( new Rect( item.RenderSize ) );