2009-08-03 72 views
2

我使用泛型与ListView控件,其初始类的定义是这样的:泛型的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(); 
     } 
    } 
} 

这里是我的设计师档案:

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 
} 

我想要做的就是创建一个Windows控件库,我已经成功完成,但是当我无法将DLL添加到工具箱时会出现问题。我不确定我为什么不能这样做。我认为所有Windows窗体控件都实现了IComponent接口,这是将项目添加到工具箱的要求。是否因为类型参数是类定义的一部分?

+1

也许缺少一个公共无参数构造函数? – Zyphrax 2009-08-03 19:28:50

回答

3

设计师讨厌:

  • 仿制药
  • 事情abstract基类

即使它的工作原理在运行时,你可能不会得到它在工作IDE。抱歉。也许考虑一个具有Type属性的非泛型类;这是关于你会做的最好...

btw,CustomListView<T>CustomListView完全不同类。你有两个班级,而不是一个班级。

2

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

对于ObjectListView我使用Adapter模式来提供对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<Person> tlist = 
    new TypedObjectListView<Person>(this.listView1); 
tlist.BindTo(myListofPeople); 

或者,而不是自己写的一切,你可以只使用ObjectListView :)

0

有可能获得一半的房子 - 我有一个在泛型类中定义的HierarchicalDataSource控件,并且让它出现在工具箱中的方式是创建一个具体的实现,尽管只是定义了类型的一个班轮。将该项目编译为一个dll,然后从该DLL中添加到工具箱中,为我提供了工具箱项目。