I’m having trouble getting a UserControl’s position on a canvas in Silverlight. Usually, I’d use this code to get the position of an object and it works fine:
GeneralTransform gt = this.TransformToVisual(Application.Current.RootVisual as UIElement);
Point offset = gt.Transform(new Point(0, 0));
double controlTop = offset.Y;
double controlLeft = offset.X;
I have a UserControl that has a method which translates its position. When I try and get the new position using the above code, it only returns the initial position the object started at. Is there a way of getting the new position for a UserControl that is programmatically added to the main canvas? I’ve tried putting this code in the mainpage as well as in the UserControl itself (and exposing it via a method).
As sod’s law goes, I’ve been working on this for a couple of hours and I finally solve it straight after posting the question.
It turns out, you have to give the UserControl a name and reference that name instead of
this. So, it would beucName.TransformToVisual.... Before, I was referencing it either by thethismethod or via the actual object itself.