2012-07-06 76 views
0

所以我有一個BorderTranslateTransform,這樣我就可以將它移動到我的窗口周圍。在Border的內容中,我有一些控件加上ScrollViewer。拖動工作正常,但是當我單擊滾動條時,整個邊框跳轉,使我的光標現在超過了我點擊的最後一個點。非常討厭,我不明白爲什麼它發生在我的滾動條上,但不是我的其他控件。圍繞窗口移動控件

(我使用的是丹尼斯·莫羅佐夫的簡單,優秀導遊http://denismorozov.blogspot.ie/2008/01/drag-controls-in-wpf-using.html

這裏是我的代碼正在做;

private void Canvas_MouseDown(object sender, MouseButtonEventArgs e) 
    { 
     this.current.X = Mouse.GetPosition((IInputElement)sender).X; 
     this.current.Y = Mouse.GetPosition((IInputElement)sender).Y; 

     // Ensure object receives all mouse events. 
     this.current.InputElement.CaptureMouse(); 
    } 

    private void Canvas_MouseUp(object sender, MouseButtonEventArgs e) 
    { 
     if (this.current.InputElement != null) 
      this.current.InputElement.ReleaseMouseCapture(); 
    } 

    private void Canvas_MouseMove(object sender, MouseEventArgs e) 
    { 
     // if mouse is down when its moving, then it's dragging current 
     if (e.LeftButton == MouseButtonState.Pressed) 
      this.current.IsDragging = true; 
     else 
      this.current.IsDragging = false; 

     if (this.current.IsDragging && current.InputElement != null) 
     { 
      // Retrieve the current position of the mouse. 
      double newX = Mouse.GetPosition((IInputElement)sender).X; 
      double newY = Mouse.GetPosition((IInputElement)sender).Y; 

      // Reset the location of the object (add to sender's renderTransform 

      // newPosition minus currentElement's position 
      Transform rt = ((UIElement)this.current.InputElement).RenderTransform; 
      double offsetX = rt.Value.OffsetX; 
      double offsetY = rt.Value.OffsetY; 
      rt.SetValue(TranslateTransform.XProperty, offsetX + newX - current.X); 
      rt.SetValue(TranslateTransform.YProperty, offsetY + newY - current.Y); 

      // Update position of the mouse 
      current.X = newX; 
      current.Y = newY; 
     } 
    } 

    public void MouseLeftBtnDown(object sender, MouseButtonEventArgs e) 
    { 
     this.current.InputElement = (IInputElement)sender; 
    } 

回答

0

實際發現它在丹尼斯頁面上的評論。問題在於不重置Canvas_MouseUp上的東西。該方法實際上應該是;

if (this.current.InputElement != null) 
{ 
this.current.IsDragging = false; 
this.current.InputElement.ReleaseMouseCapture(); 
this.current.InputElement = null; 
} 

現在就像一個魅力;)