简体   繁体   中英

Silverlight and desktop drag and drop

Is there, in silverlight, the ability to drag and drop files from the desktop into the browser? I seem to remember seeing something about it being a feature in silverlight 3.

I looked into this recently, and based on a post from a Silverlight MVP in the following thread, Silverlight 3 does not support file system drag and drop.

http://betaforums.silverlight.net/forums/t/117317.aspx?PageIndex=1

It appears Silverlight 4 now supports this:

http://www.silverlight.net/learn/videos/silverlight-4-beta-videos/silverlight-controls-drop-targets/

You can Drag and drop from desktop in a silverlight 4 and above application. Check "Require Elevated permissions" in silverlight project properties and using drop event of silverlight datagrid one can handle the drag and drop from desktop in a silverlight datagrid.

private void DocumentsDrop(object sender, DragEventArgs e)
  {
e.Handled = true;

var point = e.GetPosition(null);
var dataGridRow = ExtractDataGridRow(point);
if(dataGridRow !=null)
{.....
 }

var droppedItems = e.Data.GetData(DataFormats.FileDrop) as      FileInfo[];
if (droppedItems != null)
     {
        var droppedDocumentsList = new List<FileInfo>();

        foreach (var droppedItem in droppedItems)
        {
            if ((droppedItem.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
            {
                var directory = new DirectoryInfo(droppedItem.FullName);
                droppedDocumentsList.AddRange(directory.EnumerateFiles("*", SearchOption.AllDirectories));
            }
            else
            {
                droppedDocumentsList.Add(droppedItem);
            }
        }

        if (droppedDocumentsList.Any())
        {
            ProcessFiles(droppedDocumentsList);
        }
        else
        {
            DisplayErrorMessage("The selected folder is empty.");
        }
    }
 }

Set AllowDrop =true; in xaml for the datagrid. From the DragEventArgs extract the information as FileInfo Object. I am not sure about this working with Silverlight 3 application

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM