2015-10-05 21 views
4

假設以下情況: 一個控制(如按鈕)具有附加的行爲,以使將&落操作啓動程序如何確定拖動已結束?

<Button Content="test"> 
    <i:Interaction.Behaviors> 
     <SimpleDragBehavior/> 
    </i:Interaction.Behaviors> 
</Button> 

而且SimpleDragBehavior

public class SimpleDragBehavior: Behavior<Button> 
{  
    protected override void OnAttached() 
    {   
     AssociatedObject.MouseLeftButtonDown += OnAssociatedObjectMouseLeftButtonDown; 
     AssociatedObject.MouseLeftButtonUp += OnAssociatedObjectMouseLeftButtonUp; 
     AssociatedObject.MouseMove   += OnAssociatedObjectMouseMove; 

     mouseIsDown = false; 
    }  

    private bool mouseIsDown; 

    private void OnAssociatedObjectMouseMove (object sender, MouseEventArgs e) 
    { 
     if (mouseIsDown) 
     { 
      AssociatedObject.Background = new SolidColorBrush(Colors.Red); 

      DragDrop.DoDragDrop((DependencyObject)sender, 
           AssociatedObject.Content, 
           DragDropEffects.Link); 
     } 
    } 

    private void OnAssociatedObjectMouseLeftButtonUp (object sender, MouseButtonEventArgs e) 
    { 
     mouseIsDown = false; 
    } 

    private void OnAssociatedObjectMouseLeftButtonDown (object sender, MouseButtonEventArgs e) 
    { 
     mouseIsDown = true; 
    }  
} 

現在的任務是確定拖動何時結束,以恢復按鈕的原始背景。 這是沒有問題,當放棄目標時。但是,我如何認識一個不屬於丟棄目標的東西?在最壞的情況下:窗外?

回答

1

DragDrop.DoDragDrop返回拖放操作完成。
是「初始化拖曳和拖放操作」 混亂,因爲它可以被理解爲「開始拖和下降,並返回」:

private void OnAssociatedObjectMouseMove (object sender, MouseEventArgs e) 
{ 
    if (mouseIsDown) 
    { 
     AssociatedObject.Background = new SolidColorBrush(Colors.Red); 

     var effects = DragDrop.DoDragDrop((DependencyObject)sender, 
          AssociatedObject.Content, 
          DragDropEffects.Link); 

     // this line will be executed, when drag/drop will complete: 
     AssociatedObject.Background = //restore color here; 

     if (effects == DragDropEffects.None) 
     { 
      // nothing was dragged 
     } 
     else 
     { 
      // inspect operation result here 
     } 
    } 
} 
相關問題