Rasto
Rasto

Reputation: 17844

How to get current position of UIElement within its parent?

Lets say I have this XAML code:

<StackPanel Name="pan">
    <Button Name="but1" Content="Helllo" />
    <Button Name="but2" Content="all" />
    <Button Name="but3" Content="World" />
</StackPanel>

I my code behind I want to find out what are the coordinates of but2 within pan. How do I do it ?

Upvotes: 1

Views: 1050

Answers (1)

Pavlo Glazkov
Pavlo Glazkov

Reputation: 20756

Something like this:

private Point GetPositionOfBut2() {
   var positionTransform = but2.TransformToAncestor(pan);
   var position = positionTransform.Transform(new Point(0, 0));

   return position;
}

Upvotes: 2

Related Questions