问题描述
我有一个显示目录中文件的列表视图.
用户可以将列表视图中的每个项目拖动到文件夹/桌面,并将相关文件复制到那里.
这工作正常.问题是 - 我想为多个项目这样做 - 所以用户可以选择多个 listviewitems 并将它们拖动并复制在一起.ListView 设置为 SelectedMode=Multiple- 但它不会复制所有选定的项目.这是我的代码:
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.
This works fine. The problem is- I want to do so for multiple items- so the user can select multiple listviewitems and drag and copy them together.The ListView is set to SelectedMode=Multiple- but it doesn't copy all of the selected items.Here's my code:
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[1];
files[0] = "C:\Users\MyName\Music\My playlist\" + FileView.SelectedValue.ToString();
string dataFormat = DataFormats.FileDrop;
DataObject dataObject = new DataObject(dataFormat, files);
DragDrop.DoDragDrop(this.FileView, dataObject, DragDropEffects.Copy);
}
}
谢谢!
推荐答案
这里的问题是您使用 SelectedValue 进行多选,所以您得到一个文件.你想要的是更像这样的东西:
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);
}
}
这篇关于从 WPF 列表视图中拖动多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!