繁体   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