0

我想找到Left,Top,Right,Bottom一个UIElement. 我试过但失败了,没有结果。

这里的任何人都知道如何获得这些吗?

实际上,我正在 WPF 中创建一个自定义面板。

我不想从 Canvas 继承。

Size elemSize = this.ElementBeingDragged.DesiredSize;
// Get the element's offsets from the four sides of the Panel.
Vector v= VisualTreeHelper.GetOffset(this.ElementBeingDragged);
double left = v.X;
double right = v.X + elemSize.Width;
double top = v.Y;
double bottom = v.Y+elemSize.Height;
4

2 回答 2

1

Try

Point position = ElementBeingDragged.TranslatePoint(new Point(0, 0), UIElementRelativeTo);

e.g. getting Point relative to containing Window:

Point position = ElementBeingDragged.TranslatePoint(new Point(0, 0), Window.GetWindow(ElementBeingDragged));
于 2012-09-26T13:41:37.530 回答
0

您可以尝试使用此代码 - 基于TransformToAncestor

private Point GetPosition(Visual item) 
{
   var transformToAncestor = item.TransformToAncestor(MyForm);

   var position = transformToAncestor.Transform(new Point(0, 0));

   return position;
}
于 2012-09-26T13:36:47.443 回答