DataObject.GetDatapresent与子类的相关性是什么?

3
当我从ItemType的子类实例中调用DataObject.GetData(typeof(ItemType))方法时,该方法返回null... 我该如何从子类型获取数据?谢谢。
1个回答

7
数据对象处理不涉及类层次结构 - 它是从给定数据类型的完整名称派生出来的纯字符串“类型”,因此它没有上下文提供子类知识。我在实现树形视图中的拖放时遇到了完全相同的问题。
我有两个选项(这些可能是特定于拖放的 - 如果这不是您的问题,它可能没有太多用处) - 两者都依赖于更改数据对象的源(如果您无法访问该源,则可能没有太大用处)。
  1. Create a wrapper class which takes an ItemType instance, and when calling DoDragDrop, pass that wrapper instead of the actual instance. On the other side, test for DataObject.GetData(typeof(WrapperClass)) instead.

  2. Again, where the data object is being set, set a DataObject instance yourself - eg. call

    ctl.DoDragDrop(new DataObject(typeof(ItemType).FullName, itemTypeInstance),
        DragDropEffects.Move|DragDropEffects.Copy)
    

    then you can just use DataObject.GetData(typeof(ItemType)) on the other side.


谢谢,非常优雅简洁 :) 我在进行拖放操作时也遇到了这个问题。很难相信在使用子类进行这些操作时会有如此大的缺陷 :/ - Giacomo Tagliabue

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接