0

I want to find the Left,Top,Right,Bottom of an UIElement. I tried but failed with no result.

Can anyone here have any idea how to get these?

Actually, I am creating a custom panel in WPF.

I dont want to inherit from 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;
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154

2 Answers2

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));
Florian Gl
  • 5,984
  • 2
  • 17
  • 30
0

You can try with this code - based on TransformToAncestor

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

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

   return position;
}
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51