打开标准CollectionEditor中的“描述”面板

时间:2008-10-14 03:13:34

标签: .net visual-studio designer

我有一个具有List<T>属性的组件。列表中的类的每个属性都使用描述属性进行修饰,但描述未显示在集合编辑器中

在IDE设计器中有没有办法打开标准Collection Editor中的Description面板? 我是否需要从CollectionEditor继承我自己的类型编辑器才能实现这一目标?

1 个答案:

答案 0 :(得分:8)

基本上,您需要创建自己的编辑器或子类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()
                }
            }
        });
    }
}