WPF---将文件拖入到LISTBOX

要将文件拖入LISTBOX

1.要对XAML中的数据

<ListBox Margin="26,21,27,54" Name="listBox1" ForceCursor="False" AllowDrop="True" DragEnter="ListBox1DragEnter" DragOver="ListBox1DragOver" Drop="ListBox1DragDrop"/>

2.对于逻辑交互文件(.CS)中添加

     void ListBox1DragEnter(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.All;
        }
        void ListBox1DragOver(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.All;
        }
        void ListBox1DragDrop(object sender, DragEventArgs e)
        {

           if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
            {
                String[] files = (String[])e.Data.GetData(DataFormats.FileDrop);
                foreach (String s in files)
                {
                    //(sender as ListBox).Items.Add(new FileInfo(s));
                    FileList.Add(new FileInfo(s));
                }
            }
        }

 

这样就可以了

posted @ 2010-03-30 12:22  Just_For->Fun  阅读(524)  评论(0)    收藏  举报