问题描述
我正在使用 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 让它接受所有派生类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)