Drag multiple items from WPF listview

前端 未结 3 928
闹比i
闹比i 2021-01-20 18:15

I have a listview that displays files from a directory.
The user can drag each item in the listview to a folder/ the desktop and the associated file is copied there.

3条回答
  •  深忆病人
    2021-01-20 18:33

    The problem here is that you are using SelectedValue for a multiselect, so you get one file. What you want is something more like this:

    private void FileView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        this.start = e.GetPosition(null);
    }
    
    private void FileView_MouseMove(object sender, MouseEventArgs e)
    {
        Point mpos = e.GetPosition(null);
        Vector diff = this.start - mpos;
    
        if (e.LeftButton == MouseButtonState.Pressed &&
            (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
             Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
        )
        {
            if (this.FileView.SelectedItems.Count == 0)
                return;
    
            // right about here you get the file urls of the selected items.  
            // should be quite easy, if not, ask.  
            string[] files = new String[FileView.SelectedItems.Count];
            int ix = 0;
            foreach (object nextSel in FileView.SelectedItems)
            {
                files[ix] = "C:\\Users\\MyName\\Music\\My playlist\\" + nextSel.ToString();
                ++ix;
            }
            string dataFormat = DataFormats.FileDrop;
            DataObject dataObject = new DataObject(dataFormat, files);
            DragDrop.DoDragDrop(this.FileView, dataObject, DragDropEffects.Copy);
        } 
    }
    

提交回复
热议问题