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
751 views
in Technique[技术] by (71.8m points)

windows phone 7 - How do you find the screen position of a control in silverlight on WP7?

How do you find the screen position of a control in silverlight on WP7?

Any suggestions are much appreciated.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

If you know how it's positioned (eg in a Canvas) there may be a more appropriate way to do things, but in general, you can use UIElement.TransformToVisual to convert between the control's and the global coordinate system:

var transform = myControl.TransformToVisual(Application.Current.RootVisual)
var offset = transform.Transform(new Point(0,0))

Be sure that the control has undergone layout before trying to do this, if you do it too early the error messages are generally unhelpful.

(MSDN suggests the method is present in WP7, I don't have the SDK installed to test)


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

...