Enable Description Bar in Standard CollectionEditor

I have a component that has a property List<T>. A class in the list has every property decorated with a description attribute, but descriptions are not displayed in the collection editor.

Is there a way in the IDE designer to enable the description panel in the standard collection editor? Do I need to inherit my own type editor from CollectionEditor to achieve this?

+3
source share
1 answer

Basically, you will need to create your own editor or subclass CollectionEditorand mess up the form. The latter is simpler - but not necessarily beautiful ...

, 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()
                }
            }
        });
    }
}
+8

Source: https://habr.com/ru/post/1698237/


All Articles