如何应对GetDataPresent让它接受所有派生类型 [英] How to deal with GetDataPresent to let it accept all the derived types

查看:336
本文介绍了如何应对GetDataPresent让它接受所有派生类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 drgevent.Data.GetDataPresent 来确定被拖动组件是否是可以接受的还是不行。

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

我有一个问题,就是我要接受特定类型说SOMETYPE和所有从它派生的类型。似乎 GetDataPresent 不支持这样的要求。

I've got a problem which is that I want to accept a specific type say SomeType and all the types that derived from it. Seems GetDataPresent doesn't support such requirement.

任何想法?

推荐答案

只要不使用GetDataPresent(),它是样板,但你可以自由地做你的方式。其实检索对象和检查,如果你很高兴与它的类型:

Just don't use GetDataPresent(), it is boilerplate but you're free to do it your way. Actually retrieve the object and check if you're happy with its type:

    protected override void OnDragEnter(DragEventArgs drgevent) {
        var obj = drgevent.Data.GetData(drgevent.Data.GetFormats()[0]);
        if (typeof(Base).IsAssignableFrom(obj.GetType())) {
            drgevent.Effect = DragDropEffects.Copy;
        }
    }



其中base是基类的名称。虽然使用GetFormats的()看起来很奇怪,这种做法是保证工作,因为拖着一个.NET对象只会产生的有一个的格式,因此对象类型的显示名称。这也是GetDataPresent不能派生对象的工作的原因。

Where Base is the name of the base class. While the use of GetFormats() looks odd, this approach is guaranteed to work because dragging a .NET object only ever produces one format, the display name of the type of the object. Which is also the reason that GetDataPresent can't work for derived objects.

这篇关于如何应对GetDataPresent让它接受所有派生类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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