如何将n个拖放文件拖到显示文件路径的ListBox中? [英] How do I drag n drop files into a ListBox with it showing the file path?
问题描述
当前,当我使用Window_Drop
事件将n个拖放文件拖到我的ListBox中时,我有此代码.
Currently when I drag n drop files into my ListBox using the Window_Drop
event I have this code.
string[] files = (string[]) e.Data.GetData(DataFormats.FileDrop, true);
listBox.Items.Add(files);
在WinForm中可以正常工作,它可以打印出我刚刚拖放到其中的文件的路径,并将其作为项目放到ListBox中.
然而,当我在WPF中执行相同的操作时,却得到了
Which works fine in WinForm it prints out the path of the file I just dragged and dropped into it as a item to the ListBox.
However when I do the same thing in WPF I get this
String []数组
String[] Array
作为输出而不是路径.
现在我知道WinForm中的代码不会完全转移到WPF,但是我想它非常相似吗?
Now I know that code from WinForm doesn't exactly transfer over to WPF but I would guess it's pretty similar?
我如何正确地将一个项目拖放到具有显示文件路径的列表框?
How do I correctly drag and drop an item to the ListBox with it showing the path of the file?
推荐答案
除了将string[]
添加到列表框之外,您需要从数组的指定索引中添加一个字符串,例如listBox.Items.Add(files[yourIndex]);
Instead of adding the string[]
to the ListBox you will need to add a string from a specified index of the array like this listBox.Items.Add(files[yourIndex]);
如果要一次导入多个文件而不从同一数组中添加更多文件,则应该执行以下操作:
If you're going to import multiple files at once without adding more from the same array you should do:
foreach(string path in files)
{
listBox.Items.Add(path);
}
这篇关于如何将n个拖放文件拖到显示文件路径的ListBox中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!