2015-10-05 6 views
4

Давайте предположим следующую ситуацию: элемент управления (например, Button) имеет прикрепленную поведение, чтобы позволить Drag & Drop-OperationКак инициатор может определить, что сопротивление закончилось?

<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 
     } 
    } 
} 
Смежные вопросы