从拖动列表视图WPF多个项目 [英] Drag multiple items from WPF listview
问题描述
我有一个从目录显示文件列表视图。结果
用户可以在ListView的每个项目拖到一个文件夹/桌面和相关文件被复制那里。结果
这工作得很好。是 - 我想对多个物品─使用户可以选择多个listviewitems并拖动这样做,并复制它们在一起的问题。
ListView控件设置为SelectedMode = MULTIPLE-,但它不会复制所有选定的项目。
这里是我的code:
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多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!