Get item position after conversion

I have a UIElement that has various transformations performed on it (scaling and translation).

Is there a way to get a UIElement position after conversion? I tried GetValue(Canvas.TopProperty) , but it has not changed since it was loaded as.

I have to miss something obvious, but not sure what. (I use silverlight)

+5
source share
3 answers

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

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

TransformToVisual will give a GeneralTransform that will do the conversion from any UIElement to any other UIElement (given that they both exist in the same visual tree). It looks like you want to convert from RootVisual .

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

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

The next step is to transform the points using this transform.

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

myUiElementPosition now the Point that has been converted and should be the UIElement position you are looking for. new Point(0,0) used because I assume that you want the position to be relative to the upper left corner of RootVisual .

+17
source

Try using this approach:

 var offset = Application.RootVisual.TransformToVisual(myObject) .Transform(new Point(0,0)); 

TransformToVisual

+2
source

EDIT:

ok how about this?

 myObjectTransform.GetValue(CompositeTransform.TranslateXProperty) 

and

 myObjectTransform.GetValue(CompositeTransform.TranslateYProperty) 
-1
source

Source: https://habr.com/ru/post/898959/


All Articles