2017-07-19 2 views
0

私はコンテナにオートスクロールロジックを追加できるように、コントロールをドラッグしている間にコンテナのマウス位置を取得したい。しかし、ドラッグするとMouseMoveが全く動かず、DragOverはコントロール内をドラッグしたときにのみ起動します。wpfコントロールをドラッグしているときにコンテナ内のマウスの位置を取得する方法は?

試験例

ドラッグ可能ギズモ:

public class Gizmo : TextBlock 
    { 
     public Gizmo() 
     { 
      this.AllowDrop = true; 
      this.Background = Brushes.Gray; 
      this.Margin = new System.Windows.Thickness(6); 
     } 

     public Gizmo(string content) : this() 
     { 
      this.Text = content; 
     } 

     private bool isDragging; 
     private Point lastPressedLocation; 

     protected override void OnPreviewMouseMove(System.Windows.Input.MouseEventArgs e) 
     { 
      if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed) 
      { 
       if (!this.isDragging) 
       { 
        Point newLocation = e.GetPosition(this); 
        Vector offset = this.lastPressedLocation - newLocation; 
        if (offset.LengthSquared > 36) 
        { 
         this.lastPressedLocation = newLocation; 
         this.isDragging = true; 

         System.Windows.DragDrop.DoDragDrop(this, DateTime.Now, DragDropEffects.Move); 
        } 
        else 
        { 
         this.isDragging = false; 
        } 
       } 
      } 
     } 

     private bool canDrop; 

     protected override void OnPreviewDragEnter(DragEventArgs e) 
     { 
      Console.WriteLine("drag enter inside"); 
      if (this.Text == "gizmo 1") 
      { 
       e.Effects = DragDropEffects.Move; 
       this.canDrop = true; 
      } 
      else 
      { 
       e.Effects = DragDropEffects.None; 
       this.canDrop = false; 
      } 
      e.Handled = true; 
      base.OnPreviewDragEnter(e); 
     } 

     protected override void OnPreviewDragOver(DragEventArgs e) 
     { 
      Console.WriteLine("drag over inside"); 
      if (this.canDrop) 
      { 
       e.Effects = DragDropEffects.Move; 
      } 
      else 
      { 
       e.Effects = DragDropEffects.None; 
       e.Handled = true; 
      } 
      base.OnPreviewDragOver(e); 
     } 
    } 

コンテナ:

public class Container : WrapPanel 
    { 
     protected override void OnInitialized(EventArgs e) 
     { 
      for (int i = 1; i <= 16; i++) 
       this.Children.Add(new Gizmo(string.Format("gizmo {0}", i))); 

      base.OnInitialized(e); 
     } 

     protected override void OnPreviewDragEnter(System.Windows.DragEventArgs e) 
     { 
      Console.WriteLine("drag enter outside"); 
      base.OnPreviewDragEnter(e); 
     } 

     protected override void OnPreviewDragOver(System.Windows.DragEventArgs e) 
     { 
      //I want to get mouse postion here, but this will be called only when dragging over gizmo inside 
      Console.WriteLine("drag over outside"); 
      base.OnPreviewDragOver(e); 
     } 
    } 

running result and question

またはそれだけでは不可能ですか?

答えて

0

コードの最後の機能が動作するはずです。または(あなたの前にイベントを処理する他の要素がないので)Previewの代わりにOnDragOverメソッドを使用できます。

protected override void OnDragOver(DragEventArgs e) 
{ 
    Point position = e.GetPosition(this); 
} 

正常に動作しない場合は、通常、コントロールの特定の領域がヒットテストに表示されないことを意味します。 IsHitTestVisibletrueである必要があります(それ以外の場合は子要素も機能しません)。また、コントロールのBackgroundnullではないことを確認してください。背景を必要とせずにヒットテストを表示できる場合は、背景としてTransparentを使用します。

関連する問題