代码之家  ›  专栏  ›  技术社区  ›  benPearce

打开标准集合编辑器中的描述面板

  •  4
  • benPearce  · 技术社区  · 16 年前

    我有一个组件 List<T> 财产。列表中的类的每个属性都用一个description属性修饰,但这些描述不会显示在集合编辑器中。

    在IDE设计器中,是否可以在标准集合编辑器中打开描述面板? 我需要从CollectionEditor继承我自己的类型编辑器来实现这一点吗?

    1 回复  |  直到 12 年前
        1
  •  8
  •   wal    12 年前

    基本上,您要么需要创建自己的编辑器,要么创建子类。 CollectionEditor 把表格弄乱。后者更容易——但不一定很漂亮……

    下面使用常规集合编辑器窗体,但只扫描它 PropertyGrid 控制,启用 HelpVisible .

    /// <summary>
    /// Allows the description pane of the PropertyGrid to be shown when editing a collection of items within a PropertyGrid.
    /// </summary>
    class DescriptiveCollectionEditor : CollectionEditor
    {
        public DescriptiveCollectionEditor(Type type) : base(type) { }
        protected override CollectionForm CreateCollectionForm()
        {
            CollectionForm form = base.CreateCollectionForm();
            form.Shown += delegate
            {
                ShowDescription(form);
            };
            return form;
        }
        static void ShowDescription(Control control)
        {
            PropertyGrid grid = control as PropertyGrid;
            if (grid != null) grid.HelpVisible = true;
            foreach (Control child in control.Controls)
            {
                ShowDescription(child);
            }
        }
    }
    

    在使用中显示(注意 EditorAttribute ):

    class Foo {
        public string Name { get; set; }
        public Foo() { Bars = new List<Bar>(); }
        [Editor(typeof(DescriptiveCollectionEditor), typeof(UITypeEditor))]
        public List<Bar> Bars { get; private set; }
    }
    class Bar {
        [Description("A b c")]
        public string Abc { get; set; }
        [Description("D e f")]
        public string Def{ get; set; }
    }
    static class Program {
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.Run(new Form {
                Controls = {
                    new PropertyGrid {
                        Dock = DockStyle.Fill,
                        SelectedObject = new Foo()
                    }
                }
            });
        }
    }