0

I have spent 20 minutes trying to find an example in C# of how to draw a line between two rectangles, and I can't find anything. I don't know if I just don't understand the paradigm of drawing 2D shapes in Silverlight, or if I'm just looking in the wrong place.

I have set up the rectangles so I can drag them around, and now I want to draw a line between the two shapes as I drag a rectangle across the canvas. I want to be able to do something like this as I drag the second rectangle:

void host1_MouseLeftButtonMove(object sender, MouseEventArgs e)
{
   if (isDown) 
   {

      this.host1TranslateTransform.X = e.GetPosition(canvas).X - x;
      this.host1TranslateTransform.Y = e.GetPosition(canvas).Y - y;

      Line l = new Line();
      l.X1 = rect1.X; // does not work
      l.X2 = e.GetPosition(canvas).X;
      l.Y1 = rect1.Y; // does not work
      l.Y2 = e.GetPosition(canvas).Y;

   }
}

How do I get the coordinates of the first box? I can't figure out how to get the coordinates of shapes relative to the canvas in my application. I would appreciate any tutorials that give a beginner overview for how to draw simple 2D shapes.

Thanks!

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Dan Wolchonok
  • 1,930
  • 1
  • 21
  • 21

1 Answers1

8

try this

Canvas.GetTop(element);
Canvas.GetLeft(element);

position properties are attached properties ;)

Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184