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

查看:21
本文介绍了如何处理 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 对象只会产生 one 格式,即对象类型的显示名称.这也是 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天全站免登陆