【问题标题】:Generics ListView Custom Control泛型 ListView 自定义控件
【发布时间】:2009-08-03 19:25:20
【问题描述】:

我将泛型与 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 接口,这是将项目添加到工具箱的要求。是因为类定义中有类型参数吗?

【问题讨论】:

  • 也许缺少公共无参数构造函数?

标签: c# generics listview controls


【解决方案1】:

设计师讨厌:

  • 泛型
  • abstract 基类的东西

即使它在运行时工作,您也可能不会让它在 IDE 中工作。对不起。也许考虑一个具有Type 属性的非泛型类;这是你能做的最好的事情......

顺便说一句,CustomListView&lt;T&gt;CustomListView完全不同的类。你有 2 个类,而不是一个。

【讨论】:

    【解决方案2】:

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

    对于ObjectListView,我使用了适配器模式来提供对 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 来代替自己编写所有内容:)

    【讨论】:

      【解决方案3】:

      有可能获得半途而废 - 我有一个在泛型类中定义的 HierarchicalDataSource 控件,我让它出现在工具箱中的方式是创建一个具体的实现,尽管它只是一个带有 Types 的衬里定义。将项目编译为 dll,然后从该 DLL 添加到工具箱,即可获得工具箱项。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-25
        • 2021-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多