title: Element goes beyond its parent, canvas
body: <p>I'm trying to learn wpf. The problem is that the line I'm drawing extends beyond the canvas. This only happens when the left mouse button is pressed and before that the mouse cursor was hovering over the canvas.</p>
  <p><a href="https://i.stack.imgur.com/X2c45.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>Code .cs</p>
  <pre><code>ArrayPoints arrayPoints = new ArrayPoints(2);
      private void Canvas_mouse_move(object sender, MouseEventArgs e)
      {
  
          if(e.MouseDevice.LeftButton == MouseButtonState.Pressed)
          {
              arrayPoints.SetPoints(new Point(e.GetPosition(Canvas).X, e.GetPosition(Canvas).Y));
              if (arrayPoints.GetIndex() &gt;= 2)
              {
                  Canvas.Children.Add(new Line
                  {
                      X1 = arrayPoints.GetPoints()[0].X,
                      Y1 = arrayPoints.GetPoints()[0].Y,
                      X2 = arrayPoints.GetPoints()[1].X,
                      Y2 = arrayPoints.GetPoints()[1].Y,
                      StrokeStartLineCap = PenLineCap.Round,
                      StrokeEndLineCap = PenLineCap.Round,
                      StrokeThickness = 10,
                      Stroke = Brushes.Red
                  });
                  arrayPoints.SetPoints(new Point(e.GetPosition(Canvas).X, e.GetPosition(Canvas).Y));
              }
          }
  
      }
  
      private void Canvas_left_button_up(object sender, MouseButtonEventArgs e)
      {
          arrayPoints.ResetPoints();
      }
  </code></pre>
  <p>xaml</p>
  <pre><code>&lt;Window x:Class=&quot;Paint.MainWindow&quot;
      xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
      xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
      xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
      xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
      xmlns:local=&quot;clr-namespace:Paint&quot;
      mc:Ignorable=&quot;d&quot;
      Title=&quot;MainWindow&quot; Height=&quot;450&quot; Width=&quot;800&quot;&gt;
  &lt;Grid Background=&quot;#404040&quot;&gt;
      &lt;Canvas x:Name=&quot;Canvas&quot; Background =&quot;White&quot; Margin=&quot;30&quot; MouseMove=&quot;Canvas_mouse_move&quot; MouseLeftButtonUp=&quot;Canvas_left_button_up&quot; MouseLeave=&quot;Canvas_mouse_leave&quot;/&gt;
  
  &lt;/Grid&gt;
  </code></pre>