获取DoDragDrop DragSource [英] Obtaining DoDragDrop DragSource

查看:108
本文介绍了获取DoDragDrop DragSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF DragDrop.DoDragDrop 方法具有 DragSource 作为其第一个参数。

The WPF DragDrop.DoDragDrop method has DragSource as its first parameter.

有没有办法可以在 OnDrop 或其他拖动中获取 DragSource 对象删除事件?

Is there a way you can obtain this DragSource object in the OnDrop or other drag and drop events?

推荐答案

简短的答案是否定的,因为当您收到一个Drag事件(或DragEnter等)拖动源对象可以在任何地方。它可能在另一个过程中。它可以是本地代码。如果类似RDP的协议足够复杂,它甚至可能在另一台机器上处理它。换句话说,没有保证管理的DoDragDrop甚至被调用,如果没有保证它是从这个过程调用的。

The short answer is no, because when you receive a Drag event (or DragEnter, etc) the drag source object could be anywhere. It could be in another process. It could be in native code. It could even be on another machine if a RDP-like protocol is sophisticated enough to handle it. In other words there is no guarantee the managed DoDragDrop was even called, and if it was there is no guarantee it was called from this process.

但是/ strong>如果您正在编写调用 DoDragDrop 的代码,并且还编写了 OnDrop()的代码,那么一个简单的方法来获得这个效果:

BUT if you are writing the code that calls DoDragDrop and are also writing the code for OnDrop(), there is an easy way to get this effect:

在DoDragDrop的调用中,添加一个额外格式的对象:

In the call to DoDragDrop, add your object as an extra format:

 var dragSource = this;
 var data = "Hello";

 var dataObj = new DataObject(data);
 dataObj.SetData("DragSource", dragSource);
 DragDrop.DoDragDrop(dragSource, dataObj, DragDropEffects.Copy);

现在在OnDrag处理程序中很容易得到拖动源:

Now in the OnDrag handler it is easy to get the drag source:

protected override void OnDrop(DragEventArgs e)
{
  var data = e.Data.GetData(DataFormats.Text);
  var dragSource = e.Data.GetData("DragSource");
  ...
}

在某些情况下,知道源对象本身足以获取完成拖动操作所需的数据,在这种情况下,以上结果归结为:

In some cases, knowing the source object itself is sufficient to get the data you require to complete the drag operation, in which case the above boils down to:

 DragDrop.DoDragDrop(dragSource, dragSource, DragDropEffects.Copy);

 ...

 var dragSource = e.Data.GetData(typeof(MyDragSource));

请注意,在任何一种情况下,如果拖动操作的来源位于除代码之外(例如从Emplorer拖动文件),您将获得dragSource = null

Note that in either of these cases, if the source of the drag operation is somewhere other than your code (eg dragging a file from Emplorer), you will get dragSource=null

这篇关于获取DoDragDrop DragSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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