Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

c# - 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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...