ben*_*rce 4 .net designer visual-studio
我有一个List<T>
有财产的组件.列表中的类的每个属性都使用描述属性进行修饰,但描述不会显示在集合编辑器中
在IDE设计器中有没有办法打开标准Collection Editor中的Description面板?我是否需要从CollectionEditor继承自己的类型编辑器才能实现这一目标?
基本上,您需要创建自己的编辑器,或者子类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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
要在使用中显示此信息(请注意使用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()
}
}
});
}
}
Run Code Online (Sandbox Code Playgroud)