使用泛型创建自定义控件
我们希望创建一个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.