tags:

views:

249

answers:

2

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.

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.

Any ideas on how to write a method to do the translation of a point in RC0 that doesn't rely on MouseEventArgs.GetPosition?

+2  A: 

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.

var transform = from.TransformToVisual(to);
return transform.Transform(current);
Santiago Palladino
Fantastic Santiago! This is perfect!
Kyle Beyer
A: 

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.

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.