C# 泛型ListView自定义控件

C# 泛型ListView自定义控件,c#,generics,listview,controls,C#,Generics,Listview,Controls,我将泛型与ListView控件一起使用,其初始类定义如下所示: namespace BaseControlLibrary { public partial class CustomListView<T> : System.Windows.Forms.ListView { // Custom fields, properties, methods go here public CustomListView(List<T> d

我将泛型与ListView控件一起使用,其初始类定义如下所示:

namespace BaseControlLibrary
{
    public partial class CustomListView<T> : System.Windows.Forms.ListView
    {
        // Custom fields, properties, methods go here

        public CustomListView(List<T> data)
        {
            _columnInfo = new Dictionary<int, string>();
            _columnIndex = 0;

            _lvwItemComparer = new ListViewItemComparer();
            this.ListViewItemSorter = _lvwItemComparer;

            InitializeColumnNames();
            BindDataToListView(data);

            this.Invalidate();
        }
    }
}
TypedObjectListView<Person> tlist = 
   new TypedObjectListView<Person>(this.listView1);
tlist.BindTo(myListofPeople);
命名空间BaseControlLibrary
{
公共部分类CustomListView:System.Windows.Forms.ListView
{
//自定义字段、属性和方法位于此处
公共CustomListView(列表数据)
{
_columnInfo=新字典();
_柱状指数=0;
_lvwItemComparer=新的ListViewItemComparer();
this.ListViewItemSorter=\u lvwItemComparer;
初始化ColumnNames();
BindDataToListView(数据);
这个。使无效();
}
}
}
这是我的设计器文件:

partial class CustomListView
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    //protected override void Dispose(bool disposing)
    //{
    //    if (disposing && (components != null))
    //    {
    //        components.Dispose();
    //    }
    //    base.Dispose(disposing);
    //}

    #region Component Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        components = new System.ComponentModel.Container();
        // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    }

    #endregion
}
部分类CustomListView
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
//受保护的覆盖无效处置(布尔处置)
//{
//if(处理和(组件!=null))
//    {
//组件。Dispose();
//    }
//基地。处置(处置);
//}
#区域组件设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
components=新的System.ComponentModel.Container();
//this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
}
#端区
}
我想做的是创建一个Windows控件库,我已经成功地这样做了,但是当我无法将DLL添加到工具箱时,问题就出现了。我不太清楚为什么我不能这样做。我认为所有Windows窗体控件都实现了IComponent接口,这是向工具箱中添加项的必要条件。是因为类定义中有类型参数吗?

设计者不喜欢:

  • 仿制药
  • 具有
    抽象
    基类的事物
即使它在运行时工作,您也可能无法让它在IDE中工作。很抱歉也许考虑一个非泛型类,具有<代码>类型< /代码>属性;这大概是你能做的最好的了

顺便说一句,
CustomListView
CustomListView
完全不同的类。您有两个类,而不是一个。

您不能在设计器中使用泛型控件(即通过泛型专门化的控件)。[我似乎记得读到这是VS团队的设计决策,但我找不到参考资料。]

例如,我使用适配器模式提供对ListView控件的类型化访问

public class TypedObjectListView<T> where T : class
{
    /// <summary>
    /// Create a typed wrapper around the given list.
    /// </summary>
    public TypedObjectListView(ObjectListView olv) {
        this.olv = olv;
    }

    public void BindTo(IList<T> objects) {
       // Manipulate the attached ListView here
    }

    // plus whatever other methods you want
}
公共类TypedObjectListView其中T:class
{
/// 
///围绕给定列表创建一个类型化包装器。
/// 
公共类型ObjectListView(ObjectListView olv){
this.olv=olv;
}
公共void BindTo(IList对象){
//在此处操作附加的ListView
}
//再加上你想要的任何其他方法
}
你可以这样使用它:

namespace BaseControlLibrary
{
    public partial class CustomListView<T> : System.Windows.Forms.ListView
    {
        // Custom fields, properties, methods go here

        public CustomListView(List<T> data)
        {
            _columnInfo = new Dictionary<int, string>();
            _columnIndex = 0;

            _lvwItemComparer = new ListViewItemComparer();
            this.ListViewItemSorter = _lvwItemComparer;

            InitializeColumnNames();
            BindDataToListView(data);

            this.Invalidate();
        }
    }
}
TypedObjectListView<Person> tlist = 
   new TypedObjectListView<Person>(this.listView1);
tlist.BindTo(myListofPeople);
TypedObjectListView tlist=
新的TypedObjectListView(this.listView1);
tlist.BindTo(myListofPeople);

或者,您可以使用ObjectListView:)而不是自己编写所有内容。

有可能得到一个折衷方案-我有一个在泛型类中定义的HierarchycalDataSource控件,我让它出现在工具箱中的方法是创建一个具体的实现,尽管只是定义了类型的一行代码。将项目编译成一个dll,然后从该dll添加到工具箱中,就得到了工具箱项。

也许是因为缺少公共无参数构造函数?