Kishore Kumar
Kishore Kumar

Reputation: 12884

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

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;

Upvotes: 0

Views: 4935

Answers (2)

Florian Gl
Florian Gl

Reputation: 6014

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

Upvotes: 1

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

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

Upvotes: 0

Related Questions