Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the absolute position of an element?

Assume something simple like:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="300" />
        <ColumnDefinition Width="300" />
    </Grid.ColumnDefinitions>

    <TextBlock Name="MainTextBlock" Grid.Column="1" Text="Hello" />
</Grid>

How can I get the absolute position of MainTextBlock?

like image 758
MBZ Avatar asked Sep 12 '12 11:09

MBZ


People also ask

How do you work absolute position?

An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed). However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.

How do you get the position element XY?

The position of (X, Y) means the co-ordinate of an element at the top-left point in a document. X represent the horizontal position and Y represent the vertical position. Use element. getBoundingClientRect() property to get the position of an element.

How do you move an element with an absolute position?

Absolute Positioning You can use two values top and left along with the position property to move an HTML element anywhere in the HTML document. Move Left - Use a negative value for left. Move Right - Use a positive value for left. Move Up - Use a negative value for top.


1 Answers

I think this will work...

var ttv = MainTextBlock.TransformToVisual(Window.Current.Content);
Point screenCoords = ttv.TransformPoint(new Point(0, 0));
like image 159
Jeff Brand Avatar answered Sep 18 '22 06:09

Jeff Brand