使用泛型创建自定义控件 [英] Creating Custom Controls with generics

查看:150
本文介绍了使用泛型创建自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们希望创建一个WinForms自定义控件(从.net控件派生),并能够将其从设计器视图的工具箱中拖放。但是,只要有通用控件,我们就无法做到这一点,因为当设计者试图创建类的实例时,出于明显的原因,它不知道实例必须属于哪种类型。

We wish to create a WinForms Custom Control (which is derived from a .net control) and be able to drag and drop it from the tool box in the designer view. However we are unable to this whenever we have a control with generics because when the designer tries to create an instance of the class, for obvious reasons, it doesn't know what type the instance must be of.

有人知道解决这个问题的方法吗?

Any one knows of a way around this ?

预先感谢

======编辑========== =

======edit===========

我们想做的例子:

public class MyDataGridView<T> : DataGridView where T : class{
...
}

泛型,我们可以在设计器中看到预览,就像在普通DataGridView一样,但是正如我上面对泛型所做的解释,我们无法使用设计器。

by removing the generics we can see the preview in the designer as if it where a normal DataGridView but as I explained above with generics we are unable to use the designer.

推荐答案

我遇到了同样的问题,结果是:不要在设计器视图中使用通用控件!
您可以以编程方式创建它的实例,但没有预览。
也许在添加显式设置泛型参数的派生类时有效。

I have faced the same problem and the upshot was: Don't use generic controls in the designer view! You can programatically create instances of it, but you have no preview. Maybe it works when you add a derived class which explicitly sets the generic parameter.

这篇关于使用泛型创建自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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