How can the initiator determine that the resistance is over?

Suppose the following situation: a control (e.g. a button) has an attached behavior to enable the Drag & Drop-Operation function

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

And 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;
    }       
}

Now the task is to determine when the drag will end in order to restore the binding of the orignal button. This is not a problem when you hit the target point. But how do I recognize a fall on something that is not the target point? In the worst case scenario: outside the window?

+4
source share
1 answer

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
        }
    }
}
+1

Source: https://habr.com/ru/post/1610244/


All Articles