locked
What is the best way to get x and y coordinates of a Grid inside a Canvas

    Question

  • I have a XAML c# store app with a Canvas  and many Grid dynamically placed.I used the Grid.SetValue(canvas.leftproperty) to place the Grids and this works fine.But when I use the ManipulationDelta to allow dragging the Grids and then get the Grid.GetValue I'm not seeing the correct x and y.Please advice the best way to get the correct x and y at any time.
    Friday, May 16, 2014 7:38 AM

Answers

  • If you're moving via the RenderTransform in the ManipulationDelta then that won't affect the layout coordinates set with Grid.Left and Top. You can find the rendered location with VisualTreeHelper.FindElementsInHostCoordinates
    • Marked as answer by prasvenk Friday, May 16, 2014 9:13 PM
    Friday, May 16, 2014 1:10 PM
    Owner

All replies

  • The code you're using works for Dependency Properties, not Attached Properties.

    Did you try this:

    var left = Canvas.GetLeft(theGrid);
    var top = Canvas.GetTop(theGrid);
    

    instead of Grid.GetValue().
    Friday, May 16, 2014 9:32 AM
  • If you're moving via the RenderTransform in the ManipulationDelta then that won't affect the layout coordinates set with Grid.Left and Top. You can find the rendered location with VisualTreeHelper.FindElementsInHostCoordinates
    • Marked as answer by prasvenk Friday, May 16, 2014 9:13 PM
    Friday, May 16, 2014 1:10 PM
    Owner
  • This seems to work.but at random times the Grids reported by FindElementsInHostCoordinates are not correct. When I get this failure consistently I will get back to you.
    Friday, May 16, 2014 9:15 PM