I'm using this project as a reference: https://www.codeproject.com/Articles/168176/Zooming-and-panning-in-WPF-with-fixed-focus , but I want to limit the image from being panned off screen. I'd like panning to stop in an axis when the edge of the image is some fixed distance from the edge of the border. For example, the right edge of the image should always be at least +70 from the left edge of the border.
I've found some answered and unanswered questions, but this one seems to get me closer:
wpf-image-panning-constraints
I've modified the mouse move code as follows:
private void image_MouseMove(object sender, MouseEventArgs e)
{
if (!image.IsMouseCaptured) return;
Point p = e.MouseDevice.GetPosition(border);
Point relativePoint = image.TransformToAncestor(border)
.Transform(new Point(0, 0));
Matrix m = image.RenderTransform.Value;
var xDelta = p.X - start.X;
var yDelta = p.Y - start.Y;
m.OffsetX = origin.X + (xDelta);
m.OffsetY = origin.Y + (yDelta);
var currentImageBounds = ImageBounds();
var relativePosition = image.TransformToAncestor(border).Transform(new Point(0, 0));
if (currentImageBounds.Right <= 70 && xDelta<0)
{
//I'm messing with this code to modify/fix the x offset
m.OffsetX = origin.X;//CurrentImageBounds.Left + 70 ; //+ CurrentImageBounds.Width+70;
}
image.RenderTransform = new MatrixTransform(m);
}
private void image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (image.IsMouseCaptured) return;
image.CaptureMouse();
start = e.GetPosition(border);
origin.X = image.RenderTransform.Value.OffsetX;
origin.Y = image.RenderTransform.Value.OffsetY;
}
private Rect ImageBounds()
{
var rect = new Rect(image.RenderSize);
var bounds = image.TransformToAncestor(border).TransformBounds(rect);
//CurrentImageBounds = bounds;
Debug.WriteLine($"ImageRight: {bounds.Right}");
return bounds;
}
When I get to the 70px threshold, the image just bounces around on the edge. I'm missing something here and stuck.
How do I accomplish this?