gpt4 book ai didi

c# - 如何在 WinForms 集合编辑器中指定成员名称的来源?

转载 作者:太空宇宙 更新时间:2023-11-03 12:50:01 24 4
gpt4 key购买 nike

是否可以在下面看到的集合编辑器中更改用于获取成员名称的属性?

Image of the collections editor

[TypeConverter(typeof(ExpandableObjectConverter))]
[Serializable] // Class is also bin serialised
public class SocialPolicy
{
/// <summary>
/// The ID of the social policy
/// </summary>
[Description("The ID of the social policy")]
[DisplayName("ID"), Browsable(true), Category("General")]
[XmlElement("ID")]
public string ID { get; set; } = "POLICY_NULL";

/// <summary>
/// The name of the social policy in a display ready format
/// </summary>
[Browsable(false)]
[XmlIgnore]
public string Name { get; set; } = "Null";

/// <summary>
/// The Tree ID of the social policy
/// </summary>
[Description("The Tree ID of the social policy")]
[DisplayName("TreeID"), Browsable(true), Category("General")]
[XmlElement("TreeID")]
public string TreeID { get; set; } = "POLICYTREE_NULL";

...
}

目前它从默认值中获取其名称,即在我的编辑器中始终为“Null”的 Name 属性,但我宁愿它从 ID 中获取它。我一直在寻找任何可以用来改变它的装饰品,但一直找不到。

最佳答案

保存 SocialPolicy 列表的类可以通过使用 CollectionEditor 的自定义版本来提供它并覆盖 GetDisplayText 函数:

public class Test {

[Editor(typeof(TestEditor), typeof(UITypeEditor))]
public List<SocialPolicy> Policies { get; }

public Test() {
this.Policies = new List<SocialPolicy>();
this.Policies.Add(new SocialPolicy());
}

public class TestEditor : CollectionEditor {
public TestEditor(Type type)
: base(type) {
}

protected override string GetDisplayText(object value) {
if (value is SocialPolicy) {
return ((SocialPolicy)value).ID;
} else {
return value.ToString();
}
}
}
}

关于c# - 如何在 WinForms 集合编辑器中指定成员名称的来源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35897297/

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