WPF Datagrid拖放问题

ami*_*ros 4 wpf drag-and-drop wpfdatagrid

我有一个WPF Datagrid,我正在实现拖放功能.
datagrid有一个"文件"列表,用户可以拖动它们并将文件复制到桌面.
这样做是这样的:

string[] files = new String[myDataGrid.SelectedItems.Count];
int ix = 0;
foreach (object nextSel in myDataGrid.SelectedItems)
{
    files[ix] = ((Song)nextSel).FileLocation;
    ++ix;
}
string dataFormat = DataFormats.FileDrop;
DataObject dataObject = new DataObject(dataFormat, files);
DragDrop.DoDragDrop(this.myDataGrid, dataObject, DragDropEffects.Copy);  
Run Code Online (Sandbox Code Playgroud)

我有两个问题:
1.当我想拖多物品─这是一个问题,因为在我选择一对夫妇,并开始点击一个开始dragging-只有被选中和其他项目将取消.我尝试了这里给出的解决方案但由于某种原因它不起作用.
2.我希望在复制后从数据网格中删除拖动的项目.问题是我不知道如何检查文件是否被复制,或者用户是否只是在屏幕上拖动它而不复制它.

我希望你能帮助我解决这些问题.
谢谢!

Sef*_*fix 5

我想这就是你要找的东西:

将此代码添加到DataGrid__PreviewMouseLeftButtonDown事件处理程序:

private void DataGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    this.startingPosition = e.GetPosition(null);

    DependencyObject dep = (DependencyObject)e.OriginalSource;

    // iteratively traverse the visual tree until get a row or null
    while ((dep != null) && !(dep is DataGridRow))
    {
        dep = VisualTreeHelper.GetParent(dep);
    }

    //if this is a row (item)
    if (dep is DataGridRow)
    {
        //if the pointed item is already selected do not reselect it, so the previous multi-selection will remain
        if (songListDB.SelectedItems.Contains((dep as DataGridRow).Item))
        {
            // now the drag will drag all selected files
            e.Handled = true;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,拖延不会改变你的选择.

祝你好运!

我用那篇文章写了我的答案