2009-08-03 29 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中添加到工具箱中,爲我提供了工具箱項目。