代码之家  ›  专栏  ›  技术社区  ›  Michael Haddad

当控件位于同一项目中时,如何将扩展控件添加到工具箱?

  •  0
  • Michael Haddad  · 技术社区  · 7 年前

    我已经延长了 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);
            }
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Z.R.T.    7 年前
    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();
      
    推荐文章