Get the position of a control on a canvas RRS feed

  • Question

  • I have created a mutiple grids dynamically and added them to a canvas control. Once they have been added I allow them to be dragged and dropped around the canvas. My current problem is that I would like to no retrieve the new position of the control so I can store the new x and y co-ordinates in a database and on the next loading of the page, display them in the new position. Can anyone help me with this?

    this is my current code but it doesn't work:


     foreach (Grid child in this.dragCanvas.Children)
                    // Return the offset vector for the object.
                        Vector vector = VisualTreeHelper.GetOffset(child.Name);

                        // Convert the vector to a point value.
                        Point currentPoint = new Point(vector.X, vector.Y);

                        double left = currentPoint.X;
                        double top = currentPoint.Y;
                        double z;


    Any help will be appreciated.

    Thursday, July 29, 2010 2:01 PM


  • Can't you use Canvas.Top and Canvas.Left to get the positions? Something like:


    foreach (Grid child in this.dragCanvas.Children)
      double left = (double)child.GetValue(Canvas.LeftProperty);
      double top = (double)child.GetValue(Canvas.TopProperty);
    These would presumably be the very same properties you will want to restore when you next load the page.

    • Proposed as answer by noorbakhsh Thursday, July 29, 2010 3:19 PM
    • Marked as answer by Rayne Reid Thursday, July 29, 2010 3:43 PM
    Thursday, July 29, 2010 2:48 PM