是否可以在如下所示的集合编辑器中更改用于提供成员名称的属性?

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

[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”,但我希望它从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/

10-11 03:03
查看更多