4

我有一个具有List<T>属性的组件。列表中的类的每个属性都装饰有描述属性,但描述不会显示在集合编辑器中

在 IDE 设计器中,有没有办法在标准集合编辑器中打开“描述”面板?我需要从 CollectionEditor 继承我自己的类型编辑器来实现这一点吗?

4

1 回答 1

10

基本上,您要么需要创建自己的编辑器,要么需要创建子类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()
                }
            }
        });
    }
}
于 2008-10-14T07:22:34.283 回答