There are instances when we need the exact position of an UIElement in a silverlight application.An instant solution that comes to the mind is getPosition() of the MouseEventArgs.
But there are situations when this might not work.
Suppose I have a xaml page named Master.xaml.In that particular page I am using a menu which is again another xaml page say Menu.xaml.Both the pages have their layout container Grid,say. Now suppose inside the grid(layoutcontainer)of menu.xaml I am using number of grids to show each menu item.
What my need is I want to get the exact position of the grid for menu item but with respect to the screen ie the grid(layout container) of Master.xaml.
If I fire the MouseEventArgs for that particular grid and use the getPosition() than what I will get is (0,0) again if I use the getPosition() of grid of Master.xaml than I have to handle everything from Master.xaml which will further lead to event bubbling.So what is the better solution...............................?
Here comes GeneralTransform class comes in to the picture.
In order to get the actual position of an UIElement(in this case the grid used for menu item) in relation to a parent element(the layout grid of Master.xaml) we can use this one
GeneralTransform gt=((UIElement)childelement).TransformToVisual(par entelement);
Point p=gt.Tranform(new Point(0,0));
now p.x and p.y will give the x and y coordinate of the grid for menu.
I hope it did add some value and it was quite informative.Any suggestions are welcomed.
Thanks
Eliza