簡體   English   中英

當控件在同一項目中時,如何將擴展控件添加到工具箱?

[英]How to add an extended control to the toolbox, when the control is in the same project?

我擴展了DataGridView 我想將新控件添加到工具箱中,而不添加對其他程序集的引用(使用右鍵單擊選項“選擇項目”)。 控件位於表單的同一項目中,我不想將它們分開。 我該如何實現?

謝謝。

編輯:如果不是用戶控件,則可能無法復制有關用戶控件的問題。

編輯2:代碼本身(這是一個正在進行的工作。尚未完成):

class BindedDataGrid<T> : DataGridView
{
    public BindedDataGrid()
    {
        InitializeComponent();

        IEnumerable<PropertyInfo> properties = typeof(T).GetProperties().Where(p => Attribute.IsDefined(p, typeof(BindingValueAttribute)));

        foreach (PropertyInfo property in properties)
        {
            var column = new DataGridViewColumn()
            {
                HeaderText = ((property.GetCustomAttributes(true)[0]) as BindingValueAttribute).Value
            };

            Columns.Add(column);
        }
    }
}
public class BindedDataGrid : DataGridView
    {
        public BindedDataGrid()
        {
        }

        public BindedDataGrid(Type bindType)
        {
            IEnumerable<PropertyInfo> properties = bindType.GetProperties().Where(p => Attribute.IsDefined(p, typeof(BindingValueAttribute)));

            foreach (PropertyInfo property in properties)
            {
                var prop = property.GetCustomAttributes(true)[0];
                if (prop is BindingValueAttribute)
                {
                    var column = new DataGridViewColumn()
                    {
                        HeaderText = property.Name
                    };
                    column.CellTemplate = new DataGridViewTextBoxCell();
                    Columns.Add(column);
                }
            }
        }
    }

    public class BindingValueAttribute  : Attribute
    {
        public string Value { get; set; }
    }

    public class BindOne
    {
        [BindingValueAttribute()]
        public string Name { get; set; }

        [BindingValueAttribute()]
        public int Age { get; set; }
    }
  1. 首先使BindedDataGrid不通用
  2. 創建一個非參數化的構造函數

      private void InitializeComponent() { this.bindedDataGrid1 = new PlayingWithThread.BindedDataGrid(typeof(BindOne)); ((System.ComponentModel.ISupportInitialize)(this.bindedDataGrid1)).BeginInit(); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM