This is certainly doable. You will essentially have an image sitting on the canvas, and you will be handling MouseMove events on this canvas. When the mouse moves from one side to the other, you will use the translation of the image to move it from side to side.
The following code should get started:
Add a canvas with your image to MainPage.xaml (note the MouseMove / Enter / Leave events)
<Canvas x:Name="LayoutCanvas" MouseMove="LayoutCanvas_MouseMove" MouseEnter="LayoutCanvas_MouseEnter" Height="200" Width="200"> <Image x:Name="imgToMove" Source="myimage.png" /> </Canvas>
In your code behind add MouseMove / Enter / Leave events
Point lastMousePos = new Point(); Point currentMousePos = new Point(); double amountToMove = 1; private void LayoutCanvas_MouseMove(object sender, MouseEventArgs e) { currentMousePos = e.GetPosition(LayoutCanvas); if (lastMousePos.X > currentMousePos.X) { amountToMove--; } else { amountToMove++; } imgToMove.SetValue(Canvas.LeftProperty, amountToMove); lastMousePos = currentMousePos; } private void LayoutCanvas_MouseEnter(object sender, MouseEventArgs e) { lastMousePos = e.GetPosition(LayoutCanvas); } private void LayoutCanvas_MouseLeave(object sender, MouseEventArgs e) { imgToMove.SetValue(Canvas.LeftProperty, (double)0); }
And you're done. Now, when you mouse over the image, the image will be moved from left to right or from right to left. When you leave the image, it will return to its original position.
source share