You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让WinForms设计器中DerivableDGV的ColumnsDictionary属性像DataGridViewColumns一样展示DGVColumnDic成员

Make Custom ColumnsDictionary Property Visually Editable in Visual Studio Designer for WinForms

我明白你想要实现的效果——让自定义DerivableDGV控件的ColumnsDictionary属性在Visual Studio设计器里像内置的DataGridViewColumns一样,能直观地添加、编辑每个DGVColumnDic实例的Target和Equal值。目前的问题主要出在字段未转为属性、缺少设计器所需的序列化和编辑支持这两点上,下面是具体的修复步骤:

第一步:将DGVColumnDic的字段改为公共属性

设计器默认只会识别公共属性,不会处理公共字段。同时添加类型转换器让属性在设计器中可展开编辑:

using System.ComponentModel;

namespace ElroubyLibrary.DataGridViews
{
    // 让设计器能展开显示属性详情
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class DGVColumnDic
    {
        public string Target { get; set; }
        public string Equal { get; set; }

        public DGVColumnDic() { }

        public DGVColumnDic(string target, string equal)
        {
            Target = target;
            Equal = equal;
        }

        // 重写ToString(),让集合里的元素显示更直观的文本
        public override string ToString()
        {
            return $"Target: {Target}, Equal: {Equal}";
        }
    }
}

第二步:给ColumnsDictionary属性添加设计器支持特性

修改DerivableDGV类的属性定义,添加关键特性告诉设计器如何序列化和编辑这个数组:

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace ElroubyLibrary.DataGridViews
{
    [Designer(typeof(System.Windows.Forms.Design.ControlDesigner))]
    public class DerivableDGV : DataGridView
    {
        // 配置设计器显示和编辑规则
        [Category("Custom")] // 将属性归类到设计器的Custom选项卡
        [Description("Defines mapping between target columns and matching values")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor(typeof(CollectionEditor), typeof(UITypeEditor))]
        public DGVColumnDic[] ColumnsDictionary { get; set; } = new DGVColumnDic[0]; // 初始化空数组避免null

        public DerivableDGV() : base()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            // DerivableDGV
            this.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
            this.BackgroundColor = System.Drawing.SystemColors.ButtonHighlight;
            this.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.GridColor = System.Drawing.SystemColors.ButtonHighlight;
            this.DataSourceChanged += new System.EventHandler(this.DerivableDGV_DataSourceChanged);
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);
        }

        private void DerivableDGV_DataSourceChanged(object sender, EventArgs e)
        {
            // 你的事件逻辑
        }
    }
}

关键特性说明:

  • [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]:告诉设计器序列化属性的内容(数组里的每个元素),而非仅序列化引用。
  • [Editor(typeof(CollectionEditor), typeof(UITypeEditor))]:指定使用内置集合编辑器,提供和DataGridViewColumns一致的可视化增删改界面。
  • [Category("Custom")]:让属性在设计器属性面板中归类到自定义分组,方便用户查找。

第三步:验证效果

重新生成项目后,在WinForms设计器中拖放DerivableDGV控件:

  1. 打开属性面板找到ColumnsDictionary属性,点击右侧的...按钮弹出集合编辑器。
  2. 在编辑器中可以添加、删除DGVColumnDic实例,每个实例能展开编辑Target和Equal属性。
  3. 设计器会自动将配置序列化到窗体的InitializeComponent()代码中,运行时自动加载这些设置。

内容的提问来源于stack exchange,提问作者Magnos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 02:52:42