I am upgrading a silverlight beta 2 app to RC0 and have a function that translates a point from a child element to it's parent. The purpose of the function is to ensure that an element appears exactly on top of the child even though they are not on the same canvas and don't share a parent.
我正在将Silverlight beta 2应用程序升级到RC0,并且具有将一个点从子元素转换为它的父元素的函数。该函数的目的是确保元素恰好出现在子节点的顶部,即使它们不在同一画布上并且不共享父节点。
Here is the current function:
这是当前的功能:
protected Point TranslatePosition(Point current, Panel from, Panel to, MouseEventArgs e)
{
Point rtn = new Point(-1, -1);
// get point relative to existing parent
Point fromPoint = e.GetPosition(from);
// get point relative to new parent
Point toPoint = e.GetPosition(to);
// calculate delta
double deltaX = fromPoint.X - toPoint.X;
double deltaY = fromPoint.Y - toPoint.Y;
// calculate new position
rtn = new Point(current.X - deltaX, current.Y - deltaY);
return rtn;
}
Notice that it relies on the MouseEventArgs.GetPosition function to get the position relative to existing and new parent. In cases where there is no MouseEventArgs available, we were creating a new instance and passing it in. This was a hack but seemed to work. Now, in RC0, the MouseEventArgs constructor is internal so this hack no longer works.
请注意,它依赖于MouseEventArgs.GetPosition函数来获取相对于现有父级和新父级的位置。在没有MouseEventArgs可用的情况下,我们创建了一个新实例并将其传入。这是一个黑客但似乎工作。现在,在RC0中,MouseEventArgs构造函数是内部的,因此这个hack不再起作用。
Any ideas on how to write a method to do the translation of a point in RC0 that doesn't rely on MouseEventArgs.GetPosition?
关于如何编写一个方法来完成RC0中不依赖MouseEventArgs.GetPosition的点转换的想法?
2 个解决方案
#1
2
See TransformToVisual method of framework element. It does exactly what you want: given another control, it generates a new transform that maps the coordinates of a point relative to the current control, to coordinates relative to the passed in control.
请参见框架元素的TransformToVisual方法。它完全符合您的要求:给定另一个控件,它会生成一个新的变换,将点相对于当前控件的坐标映射到相对于传入控件的坐标。
var transform = from.TransformToVisual(to);
return transform.Transform(current);
#2
0
Yet but... There appears to be a problem with how the rendering transform pipeline accepts updates which is different to how it works in WPF.
但是......渲染转换管道如何接受与WPF中的工作方式不同的更新似乎存在问题。
I've created a wiki entry at daisley-harrison.com then talks about this. I'll turn it into a blog entry at blog.daisley-harrison.com when I get around to it later.
我在daisley-harrison.com上创建了一个wiki条目然后讨论这个。当我稍后再讨论时,我会把它变成blog.daisley-harrison.com上的博客文章。
#1
2
See TransformToVisual method of framework element. It does exactly what you want: given another control, it generates a new transform that maps the coordinates of a point relative to the current control, to coordinates relative to the passed in control.
请参见框架元素的TransformToVisual方法。它完全符合您的要求:给定另一个控件,它会生成一个新的变换,将点相对于当前控件的坐标映射到相对于传入控件的坐标。
var transform = from.TransformToVisual(to);
return transform.Transform(current);
#2
0
Yet but... There appears to be a problem with how the rendering transform pipeline accepts updates which is different to how it works in WPF.
但是......渲染转换管道如何接受与WPF中的工作方式不同的更新似乎存在问题。
I've created a wiki entry at daisley-harrison.com then talks about this. I'll turn it into a blog entry at blog.daisley-harrison.com when I get around to it later.
我在daisley-harrison.com上创建了一个wiki条目然后讨论这个。当我稍后再讨论时,我会把它变成blog.daisley-harrison.com上的博客文章。