표준 CollectionEditor에서 설명 패널을 켭니다
-
11-07-2019 - |
문제
나는 구성 요소가 있습니다 List<T>
재산. 목록의 클래스에는 설명 속성으로 장식 된 각 속성이 있지만 컬렉션 편집기에는 설명이 표시되지 않습니다.
IDE 디자이너에는 표준 컬렉션 편집기의 설명 패널을 켜는 방법이 있습니까? 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);
}
}
}
사용 중에 이것을 보여주기 위해 (사용에 주목하십시오. 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()
}
}
});
}
}
제휴하지 않습니다 StackOverflow