【问题标题】:Unable to drag file to Windows Explorer from a WPF ListView无法将文件从 WPF ListView 拖动到 Windows 资源管理器
【发布时间】:2015-12-19 22:35:29
【问题描述】:

我正在尝试拖动 ListView 项目并将其作为文件副本从该 ListView 项目中存储的位置拖放。当我开始拖动时,我成功地从 ListView 项目中获取了位置,但无法通知操作系统将该文件复制到指定位置。

    private Point start;
    ListView dragSource = null;
    private void files_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        this.start = e.GetPosition(null);

        ListView parent = (ListView)sender;
        dragSource = parent;
        object data = GetDataFromListBox(dragSource, e.GetPosition(parent));
        Hide();
        if (data != null)
        {
            string dataStr = ((UserData)data).Data.ToString();
            string filepath = new System.IO.FileInfo(dataStr).FullName;
            DataObject fileDrop = new DataObject(DataFormats.FileDrop, filepath);
            DragDrop.DoDragDrop((ListView)sender, fileDrop, DragDropEffects.Copy);
        }
    }
    private static object GetDataFromListBox(ListView source, Point point)
    {
        UIElement element = source.InputHitTest(point) as UIElement;
        if (element != null)
        {
            object data = DependencyProperty.UnsetValue;
            while (data == DependencyProperty.UnsetValue)
            {
                data = source.ItemContainerGenerator.ItemFromContainer(element);

                if (data == DependencyProperty.UnsetValue)
                {
                    element = VisualTreeHelper.GetParent(element) as UIElement;
                }

                if (element == source)
                {
                    return null;
                }
            }

            if (data != DependencyProperty.UnsetValue)
            {
                return data;
            }
        }

        return null;
    }

第二种方法GetDataFromListBox() 我在其中一个 SO 问题的答案中找到。此方法从ListBoxListView 中提取正确的数据。

我是 WPF 新手。请告诉我我错过了什么?

【问题讨论】:

    标签: c# .net wpf listview mouseevent


    【解决方案1】:

    终于在这里找到了解决办法:http://joyfulwpf.blogspot.in/2012/06/drag-and-drop-files-from-wpf-to-desktop.html

    解决方案是使用SetFileDropList() 方法分配文件下拉列表,而不是在DataObject 的构造函数中。以下是我修改后的工作代码:

    ListView parent = (ListView)sender;
    object data = parent.SelectedItems;
    
    System.Collections.IList items = (System.Collections.IList)data;
    var collection = items.Cast<UserData>();
    if (data != null)
    {
        List<string> filePaths = new List<string>(); 
        foreach (UserData ud in collection)
        {
            filePaths.Add(new System.IO.FileInfo(ud.Data.ToString()).FullName);
        }
        DataObject obj = new DataObject();
        System.Collections.Specialized.StringCollection sc = new System.Collections.Specialized.StringCollection();
        sc.AddRange(filePaths.ToArray());
        obj.SetFileDropList(sc);
        DragDrop.DoDragDrop(parent, obj, DragDropEffects.Copy);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-19
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多