Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn on Description panel in the standard CollectionEditor

I have a component which has a List<T> property. The class in the list has each of its properties decorated with a description attribute but the descriptions do not show up in the Collection Editor

In the IDE designer is there a way to turn on the Description panel in the standard Collection Editor? Will I need to inherit my own type editor from CollectionEditor to achieve this?

like image 416
benPearce Avatar asked Dec 30 '22 09:12

benPearce


1 Answers

Basically, you'd either need to create your own editor, or subclass CollectionEditor and mess with the form. The latter is easier - but not necessarily pretty...

The following uses the regular collection editor form, but simply scans it for PropertyGrid controls, enabling 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);
        }
    }
}

To show this in use (note the use of 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()
                }
            }
        });
    }
}
like image 64
Marc Gravell Avatar answered Jan 14 '23 12:01

Marc Gravell