Craig
Craig

Reputation: 1225

DataGrid Drag & Drop

I have a WPF dialog with a DataGrid on it. What I want to do is be able to drop a file or folder onto the Datagrid and have the information show up. Each row will represent each file. For the life of me I can't figure out how to insert the data and have the rows show up.

Here is the drop code...

public partial class SplitWindow : UserControl
{

    public SplitWindow()
    {
       this.InitializeComponent();
    }

    private void FilesDropped(object sender, System.Windows.DragEventArgs e)
    {

       if (e.Data.GetDataPresent(DataFormats.FileDrop))
       {
           DropDataGrid.Items.Clear(); 

           string[] droppedFilePaths = e.Data.GetData(DataFormats.FileDrop, true) as string[];

           foreach (string droppedFilePath in droppedFilePaths)
           {
               string name =          System.IO.Path.GetFileNameWithoutExtension(droppedFilePath);
               // insert row???
           }
       }
    }
}

Upvotes: 0

Views: 424

Answers (1)

mdm20
mdm20

Reputation: 4563

  1. Create an ObservableCollection<string>
  2. Set that collection as the ItemsSource for the DataGrid
  3. Add file names to collection.

You'll have to mess around with the xaml to get things to look right.

Upvotes: 1

Related Questions