从拖放事件向列表视图中添加多个文件
本文关键字:添加 文件 视图 拖放 事件 列表 | 更新日期: 2023-09-27 18:09:59
所以当我将多个文件拖拽到我的应用程序中时,我没有任何问题,可以将一个文件添加到listView中。
问题来了,当我试图添加多个文件到listView。
我希望这可能与我将文件添加到列表视图的方式有关。
当我将第一个文件拖放到我的应用程序中时,只有第一个文件被添加到listView中,我需要所有的文件都被添加到列表中。
我该怎么做?(即:我离这儿有多远?)
任何帮助都是感激的!
但无论如何……以下是我目前得到的:
<ListView x:Name="scanQueue" ItemsSource="{Binding itemList}" Margin="0,122,0,0">
<ListView.View>
<GridView>
<GridViewColumn Header="Item Name" Width="200" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Size" Width="80" DisplayMemberBinding="{Binding Size}"/>
</GridView>
</ListView.View>
</ListView>
和后面的代码:
public class items
{
public string Name { get; set; }
public string Size { get; set; }
}
public IList<items> itemList { get; set; }
public void addToList(string name, string size)
{
itemList = new List<items>()
{
new items() {Name=name, Size=size }
};
}
我认为,我在这里遇到的问题是,我试图将我的数据添加到listView的两列中。
那么,这就是魔法应该发生的方法:
private void Window_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
long byteSize = fi.Length;
string stringSize = BytesToString(byteSize);
string name = fi.Name;
addToList(name, stringSize);
}
}
我现在通读了这篇文章,意识到我没有很好地解释这个…但是我希望有人能理解我的问题。
谢谢!
好吧,这比我想象的要简单得多…现在我觉得自己很愚蠢。这可能不是我做过的最漂亮的实现。
这个小美人就是暗指我的那个!
scanQueue.Items.Add(new { itemName = name, itemSize = stringSize });
public class items
{
public string itemName { get; set; }
public string itemSize { get; set; }
}
List<string> test = new List<string>();
public void Window_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
List<string> fileList = new List<string>(files);
foreach (string file in fileList)
{
FileInfo fi = new FileInfo(file);
Console.WriteLine(fi.Name);
long byteSize = fi.Length;
string stringSize = BytesToString(byteSize);
string name = fi.Name;
scanQueue.Items.Add(new { itemName = name, itemSize = stringSize });
}
fileList.Clear();
}
我要做的第一件事是使用ObservableCollection而不是IList。第二件事是一旦你验证(string[]) e.c ata. getdata (dataformats . fileddrop);我将addToList方法更改为
public void addToList(string name, string size)
{
itemList.Add( new items() {Name=name, Size=size });
}