Since Q3 2010 we have introduced new drag mode, enabling native drag from and to your WPF applications. In this post I will show how to successfully perform drag drop operations from external program into your application through the RadDragAndDropManager API.

This can be achieved in several easy steps:

 

1. Set RadDragAndDropManager.EnableNativeDrag=”true”. This will enable native operations (if your application runs in full trust). The easiest way to do this is set it in StartUp event.

 

        private void Application_Startup(object sender, StartupEventArgs e)
       
{
           
RadDragAndDropManager.EnableNativeDrag = true;
       
}
 
2. Set the RadDragAndDropManager properties to enable the drop operation:
 
telerik:RadDragAndDropManager.AllowDrop="True"
telerik:RadDragAndDropManager.DropQuery="FileListGrid_DropQuery"
telerik:RadDragAndDropManager.DropInfo="FileListGrid_DropInfo"

 

and add the corresponding code within DropQuery and DropInfo

 

  private void FileListGrid_DropQuery(object sender, DragDropQueryEventArgs e)
       
{
           
if (!RadDragAndDropManager.IsDragging && e.Options.DropDataObject.ContainsFileDropList())
           
{
               
e.QueryResult = true;
           
}
       
}

       
private void FileListGrid_DropInfo(object sender, DragDropEventArgs e)
       
{
           
if (e.Options.Status == DragStatus.DropComplete && !RadDragAndDropManager.IsDragging && e.Options.DropDataObject.ContainsFileDropList())
           
{
               
var items = ((RadGridView)e.Options.Destination).ItemsSource as IList;
               
var droppedFiles = e.Options.DropDataObject.GetFileDropList();

               
if (items != null && droppedFiles != null)
               
{
                   
foreach (var droppedFile in droppedFiles)
                   
{
                       
items.Add(droppedFile);
                   
}
               
}
           
}
       
}

 

In the attached sample I have added RadGridView as well as RadTreeListView, that accepts files from the file system.

Download sample


About the Author

Tsvyatko Konov

Tsvyatko Konov is the Lead of UI for Xamarin and UI for UWP team. He is keen on sharing technical experience through mentoring and team development as well as in publications and lectures. In his spare time, TK loves sailing and skiing.

Related Posts

Comments

Comments are disabled in preview mode.