views:

224

answers:

2

Hello,
I have a Canvas inside a ScrollViewer; the Canvas will have several objects drawn on it.
I would like to be able to Pan -- scroll -- the Canvas using the mouse: LButtonDown - move mouse - LButtonUp.
In the .xaml, I have a TranslateTransform for the Canvas.
Code:

private void MapCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
        ClickPosition = e.GetPosition(MapCanvas);
        Debug.WriteLine("LButtonDown: " + ClickPosition.ToString());
        MapCanvas.CaptureMouse();
        IsMouseCaptured = true;

        e.Handled = true;
 }
 private void MapCanvas_MouseMove(object sender, MouseEventArgs e)
 {
     if (IsMouseCaptured)
     {
         Point point = new Point(e.GetPosition(MapCanvas));
         Debug.WriteLine("\t" + point.ToString());
         MapPanTransform.X = point.X - ClickPosition.X;
         MapPanTransform.Y = point.Y - ClickPosition.Y;
     }
 }

This results in some strange points appearing in MapCanvas_MouseMove:
LButtonDown 557,469
556,469
368,472
555,469
367,472
554,469
365,472
553,469

Any ideas on what's going on? Thanks.

A: 

Are there any controls that overlay the Canvas you're trying to move? You might look into the isHitTestVisible property. If I understand correctly it makes mouse events transparent on a control so that all events happen on controls "below" the control where isHitTestVisible is false.

Dave Swersky
I don't think that's the issue. Looking at the debugging output, the bold numbers are decreasing at the same rate as the non-bold, as though the MouseMove event was being sent w/r/t to a different origin.
Number8
It appears to be related to the TranslateTransform (MapPanTransform). If I comment out those lines, the MouseMove debugging output looks like what I expect. Of course, the map doesn't get scrolled...
Number8