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

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

问题描述

我有一个 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 的转换(假设它们都存在于同一可视化树中).听起来您想要的是 RootVisual 的转换.

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

transform 对象现在是一个通用转换,可用于以与 myUiElement 相对于 RootVisual 的转换相同的方式转换任何内容

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 现在是一个经过转换的 Point 并且应该是您正在寻找的 UIElement 的位置.使用 new Point(0,0) 是因为我假设您希望相对于 RootVisual 的左上角给出位置.

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天全站免登陆