获取元素位置转换后

编程入门 行业动态 更新时间:2024-10-25 20:29:49
本文介绍了获取元素位置转换后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有了上进行的各种转换的的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.

更多推荐

获取元素位置转换后

本文发布于:2023-10-07 23:46:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1470880.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   位置

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!