Here's an idea of how to get started with this
<Canvas x:Name="ImageCanvas" MouseMove="Canvas_MouseMove" MouseLeftButtonDown="Canvas_MouseDown" MouseLeftButtonUp="Canvas_MouseUp">
<Image x:Name="moveImage" Source="...." />
</Canvas>
Here we've created a Canvas that we are going to use to pan the image around. Next we need to hook up to some code in the code behind. Don't worry if this doesn't seem to be MVVM - the point of MVVM is not to abstract display code from the code behind:
Point mousePos = new Point();
Point currentPos = new Point();
double moveAmountX = 1;
double moveAmountY = 1;
bool isMouseDown = false;
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
if (!isMouseDown) return;
currentPos = e.GetPosition(ImageCanvas);
if (mousePos.X > currentPos.X)
{
moveAmountX--;
}
else if (mousePos.X < currentPos.X)
{
moveAmountX++;
}
if (mousePos.Y > currentPos.Y)
{
moveAmountY--;
}
else if (mousePos.Y < currentPos.Y)
{
moveAmountY++;
}
moveImage.SetValue(Canvas.LeftProperty, moveAmountX);
moveImage.SetValue(Canvas.TopProperty, moveAmountY);
mousePos = currentPos;
}
private void Canvas_MouseDown(object sender, MouseEventArgs e)
{
mousePos = e.GetPosition(ImageCanvas);
isMouseDown = true;
}
private void Canvas_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}
In this, we capture the mouse in the
MouseLeftButtonUp
event and release it in the
MouseLeftButtonUp
event. Finally, we compare mouse positions in the mouse move and adjust the image accordingly. I've just knocked this together in the editor here on Code Project, so you may need to adjust the direction of the tests, but this should get you started.