Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get element position after transform

I have a UIElement that has various transformations performed on it (scale and translate).

Is there a way to get the UIElement's position after transformation? I tried GetValue(Canvas.TopProperty) but it doesn't change from what it was loaded as.

I must be missing something obvious but not sure what. (I'm using silverlight)

like image 383
Skoder Avatar asked Mar 10 '11 22:03

Skoder


People also ask

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.

What does transform translateX do?

The translateX() function is a 2D transform function used to translate an element along the x-axis. It takes a translation value tx as an argument. This value specifies the amount by which an element is to be translated. The translation value tx is provided either as a <length> or as a percentage .

How do you position an element on top of another?

You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).


1 Answers

It is a little bit unintuitive to do this, but it can be done. It is a two step process. First, what you want to use the TransformToVisual function (from MSDN):

Returns a transform object that can be used to transform coordinates from the UIElement to the specified object.

TransformToVisual will yield a GeneralTransform that will perform the transformation from any UIElement to any other UIElement (given that they both exist in the same visual tree). It sounds like what you want is the transformation from the RootVisual.

var transform = Application.RootVisual.TransformToVisual(myUiElement);

The transform object is now a general transformation that can be used to transform anything in the same way that myUiElement was transformed relative to RootVisual

The next step is to transform a point using that transformation.

var myUiElementPosition = transform.Transform(new Point(0,0));

The myUiElementPosition is now a Point that has been transformed and should be the position of the UIElement you're looking for. new Point(0,0) is used because I assume you want to have the position given relative to the top left corner of the RootVisual.

like image 137
Adam Price Avatar answered Sep 20 '22 01:09

Adam Price