title: Moving the dynamically drawn rectangle inside the canvas using MouseMove event
body: <p>I'm trying to move a dynamically drawn rectangle inside the canvas. I’m able to draw the rectangle dynamically with in the canvas and while trying to move the rectangle inside the canvas I’m facing problem </p>
  
  <p><strong>XAML :</strong></p>
  
  <pre><code>   &lt;Grid x:Name="Gridimage1" Margin="0,0,411,100"&gt;
        &lt;Image Name="image1" HorizontalAlignment="Left" Stretch="Fill" VerticalAlignment="Top"&gt;&lt;/Image&gt;
          &lt;Canvas x:Name="BackPanel" Margin="20,67,0,0" Height="317" Width="331"&gt;
             &lt;Rectangle x:Name="selectionRectangle" Stroke="LightBlue" Fill="#220000FF"/&gt;
          &lt;/Canvas&gt;
     &lt;/Grid&gt;
  </code></pre>
  
  <p><strong>C# :</strong></p>
  
  <p>After drawing the rectangle dynamically I'm adding the below mouse events.</p>
  
  <pre><code>selectionRectangle.MouseLeftButtonDown += new MouseButtonEventHandler(Rect1_MouseDown);
  selectionRectangle.MouseMove += new MouseEventHandler(Rectangle_MouseMove_1);
  selectionRectangle.MouseUp += new MouseButtonEventHandler(Rect1_MouseUp); 
       # region "rectangle move"
      private bool drag = false;
      private Point startPt;
      private int wid;
      private int hei;
      private Point lastLoc;
      private double CanvasLeft, CanvasTop;
      private void Rect1_MouseDown(object sender, MouseButtonEventArgs e)
      {
          drag = true;
          Cursor = Cursors.Hand;
          startPt = e.GetPosition(BackPanel);
          wid = (int)selectionRectangle.Width;
          hei = (int)selectionRectangle.Height;
          lastLoc = new Point(Canvas.GetLeft(selectionRectangle), Canvas.GetTop(selectionRectangle));
         Mouse.Capture((IInputElement)sender);
      }
  
      private void Rectangle_MouseMove_1(object sender, MouseEventArgs e)
      {
          try
          {
              if (drag)
              {
                      var newX = (startPt.X + (e.GetPosition(BackPanel).X - startPt.X));
                      var newY = (startPt.Y + (e.GetPosition(BackPanel).Y - startPt.Y));
                      Point offset = new Point((startPt.X - lastLoc.X), (startPt.Y - lastLoc.Y));
                      CanvasTop = newY - offset.Y;
                      CanvasLeft = newX - offset.X;
                      selectionRectangle.SetValue(Canvas.TopProperty, CanvasTop);
                      selectionRectangle.SetValue(Canvas.LeftProperty, CanvasLeft);               
  
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);    
          }
  
      }
      private void Rect1_MouseUp(object sender, MouseButtonEventArgs e)
      {
          drag = false;
          Cursor = Cursors.Arrow;
          Mouse.Capture(null);
      }
      #endregion
  </code></pre>
  
  <p><strong>Problem:</strong> I'm able to move the rectangle all over the window. I want only to move the rectangle inside the canvas margin.</p>
  
  <p><img src="https://i.stack.imgur.com/rZUKH.gif" alt="enter image description here"></p>
  
  <p>I'm able to move the rectangle outside the canvas</p>
  
  <p><img src="https://i.stack.imgur.com/d1PRN.gif" alt="enter image description here"></p>