WPF drag and drop to/from Windows Explorer but not to/from self

-1

I have a radGridView to which I can drop files from the Windows Explorer, and from which I can also drag files to Explorer.

However, I need to forbid dragging into itself (duplicating the entries and also generating exceptions).

WPF:

<telerik:RadGridView Name="radGridView" Drop="OnDrop" DragLeave="radGridView_DragLeave">
    <telerik:RadGridView.RowStyle>
        <Style TargetType="telerik:GridViewRow">
            <Setter Property="telerik:DragDropManager.AllowDrag" Value="True" />
        </Style>
    </telerik:RadGridView.RowStyle>

And handlers:

private void OnDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
        MessageManager.Publish("LoadDataFiles", files);
    }
}

private void radGridView_DragLeave(object sender, DragEventArgs e)
{
    [..]
    DragDrop.DoDragDrop(this, new DataObject(DataFormats.FileDrop, paths.ToArray()),
    DragDropEffects.Copy);
}

This code does work, however, I've failed to avoid drag-drop into itself. Also, the icon when dropping into the file explorer is not right.

Notably OnDrop gets called sometimes even if the files are dropped into the explorer.

It also sometimes generates an exception after the drop from the gridview to the file explorer, exception happens on the DragDrop line:

Managed Debugging Assistant 'FatalExecutionEngineError' : 'The runtime has encountered a fatal error. The address of the error was at 0x70cca7d0, on thread 0x48c8. The error code is 0x80131623. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.'

c#
.net
wpf
drag-and-drop
asked on Stack Overflow Jan 22, 2020 by Damien • edited Feb 2, 2020 by Keith Stein

1 Answer

0

Determining whether the data being dragged is acceptable by the target is done during the DragOver event. You should handle radGridView.DragOver and set DragEventArgs.Effects to None if the data being dropped is already present in the grid.


The DragLeave event you seem to be handling:

Occurs when an object is dragged out of the bounds of an element that is acting as a drop target without being dropped.

By the time this event is being raised, a drag/drop operation is already in progress. But in your handler you call DragDrop.DoDragDrop, which is what you're supposed to call to first start a drag/drop. Since one is already in progress, I'm not surprised this would throw some kind of exception.

I'm not sure what advice I can give on this other than to take another look at the WPF Drag and Drop Overview and make sure you understand which events do what.


As for "the icon when dropping into the file explorer is not right". I can't do much without you showing me what "not right" means. I suspect that you want to show some kind of custom mouse cursor, so I'll tell you that this is suppose to be done as part of the GiveFeedback event.

answered on Stack Overflow Feb 2, 2020 by Keith Stein

User contributions licensed under CC BY-SA 3.0