title: Prevent WPF Image in border from panning too far
body: <p>I have a WPF <code>Image</code> inside a <code>Border</code> I currently am able to click and drag to pan the image. I want to prevent users from dragging the image off the screen. Perhaps a minimum of 100 pixels should be showing at any border. That is, if a user drags the image all the way to the left, most of the image will disappear, but 100 pixels will still be hanging out toward the left boundary of the border.</p>
  
  <p>Here's my current code for the MouseMove and LeftClick event:</p>
  
  <pre><code>private void img_Box_MouseMove(object sender, MouseEventArgs e)
          {
              if (!img_Box.IsMouseCaptured) return;
              var tt = (TranslateTransform)((TransformGroup)img_Box.RenderTransform).Children.First(tr =&gt; tr is TranslateTransform);
              Vector v = start - e.GetPosition(img_Border);
              tt.X = origin.X - v.X;
              tt.Y = origin.Y - v.Y;          
          }
  
  private void img_Box_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
      {
          img_Box.CaptureMouse();
          var tt = (TranslateTransform)((TransformGroup)img_Box.RenderTransform).Children.First(tr =&gt; tr is TranslateTransform);
          start = e.GetPosition(img_Border);
          origin = new System.Windows.Point(tt.X, tt.Y);
      }
  </code></pre>
  
  <p>I think I should be able to put a pretty simple condition on that to achieve my desired result, but I can't figure out what the condition should be.</p>