Draggable Canvas in WPF Using a 'Thumb'

On my quest to implement a very simple 'drag' mechanism to my application (which consists of multiple canvasses nested inside a 'parent' canvas) I've come up with the following bits of code:

(Showing only the relevant bits to save space)

MainWindow.xaml

    
        
            
        

        
        

MainWindow.xaml.cs

    void onDragDelta(object sender, DragDeltaEventArgs e)
    {
        Canvas.SetLeft(myCanvas, e.HorizontalChange);
        Canvas.SetTop(myCanvas, e.VerticalChange);

        //debug info
        pos.Content = "Left: " + Canvas.GetLeft(myCanvas) + ", Top: " + Canvas.GetTop(myCanvas);
        changes.Content = "Horizontal: " + e.HorizontalChange + ", Vertical: " + e.VerticalChange;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Canvas.SetLeft(myCanvas, 0);
        Canvas.SetTop(myCanvas, 0);
    }

Which seems to work, randomly! It seems to follow the mouse movements but I am having trouble interpreting the DragDeltaEventArgs, which causes a very unstable movement of the canvas. It's kind of hard to explain so here is a short video I've captured of it: http://img84.imageshack.us/img84/6614/drag.mp4

Any comments/suggestions will be much appreciated as I've been staring at this for a while and can't figure out what to do with it :(

5
задан Rudi Visser 11 February 2013 в 16:39
поделиться