How to deal with GetDataPresent to let it accept all the derived types

后端 未结 2 1863
醉话见心
醉话见心 2020-12-15 10:09

I\'m using drgevent.Data.GetDataPresent to determine whether the dragged component is acceptable or not.

I\'ve got a problem which is that I want to accept a specifi

2条回答
  •  自闭症患者
    2020-12-15 10:47

    I have answered a similar question previously: C# Drag and Drop - e.Data.GetData using a base class

    What you can do is create a container class which holds the data that you are dragging. And then in the GetDataPresent you check for the container class type and if it is present then you can read the content member which contains the actual instance of your data.

    Here is an quick example, if your base type is DragDropBaseData, you can create the following DragDropInfo class.

    public class DragDropInfo 
    { 
      public DragDropBaseData Value { get; private set; } 
    
      public DragDropInfo(DragDropBaseData value) 
      { 
        this.Value= value; 
      } 
    }
    

    And then the drag drop can be initiated with the following, where DrafDropDerivedData is a class derived from DragDropBaseData.

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

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

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

提交回复
热议问题