gpt4 book ai didi

.net - 在标准 CollectionEditor 中打开“描述”面板

转载 作者:行者123 更新时间:2023-12-02 20:39:01 26 4
gpt4 key购买 nike

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

在 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()
}
}
});
}
}

关于.net - 在标准 CollectionEditor 中打开“描述”面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/199897/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com