繁体   English   中英

如何在WPF中获取UIElement的Left,Top,Right,Bottom点?

[英]How to get the Left,Top,Right,Bottom point of a UIElement in WPF?

我想找到UIElementLeft,Top,Right,Bottom 我尝试过但失败了,但没有结果。

这里有人可以知道如何获得这些吗?

实际上,我正在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;

尝试

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

例如,获取Point相对于包含Window的信息:

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

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

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

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

   return position;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM