How to get the absolute position of an element?

MBZ picture MBZ · Sep 12, 2012 · Viewed 19k times · Source

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?

Answer

Jeff Brand picture Jeff Brand · Sep 12, 2012

I think this will work...

var ttv = MainTextBlock.TransformToVisual(Window.Current.Content);
Point screenCoords = ttv.TransformPoint(new Point(0, 0));