xamldatagriddrag-and-dropsilverlight-5.0componentone

drag drop in c1datgrid from desktop


I have an application in silverlight 5. I use c1datagrid in our application. I want to drag and drop items from Desktop to our c1datagrid rows and while doing that I want to highlight that particular row in which I am dropping.


Solution

  • One can Drag and drop in a silverlight 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 provided its not an OOB silverlight application.

    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.