获取元素位置转换后 [英] Get element position after transform

查看:119
本文介绍了获取元素位置转换后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了上进行的各种转换的的UIElement(规模和翻译)。

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

有没有办法让改造后的UIElement的地位?我试图的GetValue(Canvas.TopProperty),但它不从它加载的变化。

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.

我必须失去了一些东西明显,但不知道是什么。 (我使用Silverlight)

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

推荐答案

有直观做到这一点,但它可以做到的。它是一个两步过程。首先,你要使用 TransformToVisual 函数(什么的从MSDN ):

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):

返回一个变换对象,可以被用来从的UIElement的坐标转换为指定的对象。

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

TransformToVisual 将产生 GeneralTransform 将执行从任意的UIElement转变到任何其他的UIElement(因为它们都在同一个可视化树存在)。这听起来像你想要的是从改造 RootVisu​​al

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);



变换对象现在是一个普遍的转型可以使用同样的方式, myUiElement 是相对于 RootVisu​​al

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));



myUiElementPosition 现在是一个已转换,应该是的UIElement 您正在寻找的位置。 新点(0,0)的使用,因为我假设你想拥有相对给予的左上角的位置 RootVisu​​al

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.

这篇关于获取元素位置转换后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆