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

时间:2017-10-03 16:49:05

标签: c# winforms visual-studio controls toolbox

我延长了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 个答案:

答案 0 :(得分:0)

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();