Drag-and-Drop from RadGridView to Outlook

1 Answer 148 Views
DragAndDrop GridView
Ict
Top achievements
Rank 1
Iron
Ict asked on 19 Oct 2022, 08:22 AM

I want to drag and drop files from a RadGridView to Outlook. Each line in my grid represents an Invoice including PDF file.
My users want to drag items into an Outlook message in order to send the PDF as attachment. (one or more)

I've been trying to search for a way to do it, but I can only find examples to drag-and-drop within the same application.

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 21 Oct 2022, 11:03 AM

Hello,

The drag/drop code examples related to external drop in WPF are a bit scarce. This is mostly because WPF doesn't know anything of the external application and you cannot fully control the drag/drop operation. The only thing that you can do is to prepare the drag data object expected by the other application and provide it to the drag/drop operation which internally works with the Windows APIs. If the external application knows how to handled prepared data object on drop, then your code will work. 

Because in the different applications you will have different data types and implementations and most of them are hidden, you won't know what types to provide to the drag/drop operation. This is why .NET provides a common API for several common data formats (like images and files). A lot of applications (like Outlook and most browsers for example) use the FileDrop data format, which means that you can use it to transfer files on drag/drop. This sounds like something that should work in your case.

To achieve your requirement, you can subscribe the GridView to the DragDropManager.DragInitialize event. In the event handlers, you can prepare a list of file paths (ex: C:\Invoices\Invoice_N1.pdf) and provide this to the Data property of the arguments.

DragDropManager.AddDragInitializeHandler(this.gridView, OnGridViewDragInitialized);

private void OnGridViewDragInitialized(object sender, DragInitializeEventArgs e)
{
	var draggedItems = this.gridView.SelectedItems.OfType<Invoice>(); 
	string[] files = draggedItems.Select(x => x.FilePath).ToArray(); 
	var data = new DataObject(DataFormats.FileDrop, files);
	e.AllowedEffects = DragDropEffects.Copy;
	e.Data = data;
}

I also attached a small sample project showing this approach. I hope it helps.

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Ict
Top achievements
Rank 1
Iron
commented on 21 Oct 2022, 11:34 AM

This already helps alot. Managed to get it working with Outlook. Is there a more "MVVM-Friendly" way to do it? Or can it only be managed via the code behind?
Martin Ivanov
Telerik team
commented on 21 Oct 2022, 11:37 AM

You can implement it in a behavior similar to the one in the following SDK example: https://github.com/telerik/xaml-sdk/blob/master/GridView/DragDropWithLines/RowReorderBehavior.cs

This way you can avoid using code-behind and also the behavior can be re-used if you create the solution more generic.

Tags
DragAndDrop GridView
Asked by
Ict
Top achievements
Rank 1
Iron
Answers by
Martin Ivanov
Telerik team
Share this question
or