如何从任意源拖放到我的WPF应用程序中? [英] How do I drag-and-drop from an arbitrary source into my WPF application?

查看:89
本文介绍了如何从任意源拖放到我的WPF应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含迷你文件浏览器的WPF应用程序。我希望能够从各种来源(可能是资源管理器或电子邮件程序中的电子邮件附件)拖动文件,并将其放在我的文件浏览器控件上。文件的类型无关紧要,因为操作的语义总是相同的:将文件复制到我的迷你文件浏览器控件显示的当前目录中。



我该怎么做?我已经阅读了互联网上的大量内容,但它们都是从处理鼠标拖动开始拖拽开始的。我对此没有任何控制权,因为这发生在许多其他应用程序之一。



任何帮助将不胜感激。



我尝试了什么:



我试过设置 Drop =我的目标控件上的True并为 Drop 事件提供处理程序,但是当我拖动时,控件只显示一个无条目图标文件到它并且事件不会触发。

解决方案

你需要处理 DragDrop.DragOver附加事件 [ ^ ]并设置效果基于可用数据格式的 DragEventArgs 参数的属性为适当的值。

  private   void  control_DragOver( object  sender,DragEventArgs e) 
{
e.Effects = DragDropEffects.None;

if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effects = DragDropEffects。复制;
}
}

private void control_Drop( object sender,DragEventArgs e)
{
string [] droppedFiles = e。 Data.GetData(DataFormats.FileDrop) as string [];
if (droppedFiles!= null && droppedFiles.Length!= 0
{
...
}
}



拖放概述 [ ^ ]



我没用过,但我刚刚找到这个WPF拖放库 [ ^ ],看起来很有希望。


I have a WPF application that has a mini-file-explorer. I want to be able to drag files from various sources (maybe Explorer or maybe an email attachment in an email program) and drop them on my file-explorer control. The type of the file does not matter because the semantics of the operation are always the same: copy the file into the current directory being displayed by my mini-file-explorer control.

How do I do this? I have read loads of stuff on the internet, but they all begin with the handling of the mouse-drag that initiates the drag-and-drop. I don't have any control over this because this occurs in one of a number of other applications.

Any help would be greatly appreciated.

What I have tried:

I have tried setting Drop="True" on my target control and providing a handler for the Drop event, but the control simply displays a no-entry icon when I drag a file onto it and the event does not fire.

解决方案

You'll need to handle the DragDrop.DragOver attached event[^] and set the Effects property on the DragEventArgs parameter to an appropriate value based on the available data formats.

private void control_DragOver(object sender, DragEventArgs e)
{
    e.Effects = DragDropEffects.None;

    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        e.Effects = DragDropEffects.Copy;
    }
}

private void control_Drop(object sender, DragEventArgs e)
{
    string[] droppedFiles = e.Data.GetData(DataFormats.FileDrop) as string[];
    if (droppedFiles != null && droppedFiles.Length != 0)
    {
        ...
    }
}


Drag and Drop Overview[^]

I haven't used it, but I've just found this WPF drag/drop library[^], which looks promising.


这篇关于如何从任意源拖放到我的WPF应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆