punker76 / gong-wpf-dragdrop

The GongSolutions.WPF.DragDrop library is a drag'n'drop framework for WPF
BSD 3-Clause "New" or "Revised" License
2.31k stars 401 forks source link

Drag Adorners don't work when dragging items from outside the app #226

Closed mhmd-azeez closed 7 years ago

mhmd-azeez commented 7 years ago

I need to drag and drop files from Windows Explorer into my main window. Which works fine. But the drag adorners don't work. It just shows the standard windows cursors. I tried it on the sample app too, doesn't work there either.

punker76 commented 7 years ago

@encrypt0r You can implement the IDropTarget interface on your view or view model and handle this.

public void DragOver(IDropInfo dropInfo)
{
    dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;

    var dataObject = dropInfo.Data as IDataObject;
    // look for drag&drop new files
    if (dataObject != null && dataObject.GetDataPresent(DataFormats.FileDrop))
    {
        dropInfo.Effects = DragDropEffects.Copy;
    }
    else
    {
        dropInfo.Effects = DragDropEffects.Move;
    }
}

public void Drop(IDropInfo dropInfo)
{
    var dataObject = dropInfo.Data as DataObject;
    // look for drag&drop new files
    if (dataObject != null && dataObject.ContainsFileDropList())
    {
        this.HandleDropActionAsync(dropInfo, dataObject.GetFileDropList());
    }
    else
    {
        GongSolutions.Wpf.DragDrop.DragDrop.DefaultDropHandler.Drop(dropInfo);
        var data = dropInfo.Data;
        // do something with the data
    }
}