C#拖放 - e.Data.GetData使用基类 [英] C# Drag and Drop - e.Data.GetData using a base class

查看:562
本文介绍了C#拖放 - e.Data.GetData使用基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#和Winforms 3.5



我有一个用户控件的列表,全部派生自一个基类。这些控件可以添加到各种面板,我正在尝试实现拖放功能,我正在运行的问题是DragDrop事件。



对于DragEventArgs e.Data.GetData(typeof(baseClass))不起作用。它想要:

  e.Data.GetData(typeof(derivedClass1))
e.Data.GetData(typeof derivedClass2))
etc ...

有没有办法可以解决这个问题,或者更好的构建它的方法?

解决方案

您可以将数据包装在一个公共类中。例如,假设你的基类被称为DragDropBaseControl

  public class DragDropInfo 
{
public DragDropBaseControl Control {得到;私人集合

public DragDropInfo(DragDropBaseControl control)
{
this.Control = control;
}
}

然后可以使用以下内容启动拖放在基础类中

  DoDragDrop(new DragDropInfo(this),DragDropEffects.All); 

您可以使用以下


访问拖动事件中的数据

  e.Data.GetData(typeof(DragDropInfo)); 

我是否正确理解了您的要求?


I am using C# and Winforms 3.5

I have a list of user controls all derived from one base class. These controls can be added to various panels and I'm trying to implement the drag-drop functionality, the problem I'm running in to is on the DragDrop event.

For DragEventArgs e.Data.GetData(typeof(baseClass)) doesn't work. It wants:

e.Data.GetData(typeof(derivedClass1))
e.Data.GetData(typeof(derivedClass2))
etc...

Is there a way I can get around this, or a better way to architect it?

解决方案

You can wrap the data in a common class. For example, assuming your base class is called DragDropBaseControl

public class DragDropInfo
{
  public DragDropBaseControl Control { get; private set; }

  public DragDropInfo(DragDropBaseControl control)
  {
    this.Control = control;
  }
}

And then the drag drop can be initiated with the following in the base class

DoDragDrop(new DragDropInfo(this), DragDropEffects.All);

And you can access the data in the drag events using the following

e.Data.GetData(typeof(DragDropInfo));

Have I understood your requirement correctly?

这篇关于C#拖放 - e.Data.GetData使用基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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