属性 网格 Collection 成员名称

Property Grid Collection Member name

有没有办法编辑 Collection 编辑器以及某些值显示为什么。我的 collection 的成员在向用户显示它时我想要一个对用户更友好的名称,而不是解决方案名称加上 class 作为显示的名称。有什么办法吗?

您说“...解决方案名称加上 class 名称...”并用 "propertygrid" 标记了您的问题。我假设您在 WindowsFormsApplication 项目中遇到设计时问题。看看下面的例子能不能帮到你。

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace WindowsFormsApplication1
{
    // Example custom control
    [Docking(System.Windows.Forms.DockingBehavior.Ask)]
    class MyControl : Control
    {
        public MyControl()
        {
            BackColor = Color.White;
        }

        // Example array property
        [Editor(typeof(MyCollectionEditor), typeof(UITypeEditor))]
        public MyObject[] MyObjectArray
        {
            get;
            set;
        }
    }

    // This class requires System.Design assembly to be included in the project
    class MyCollectionEditor : System.ComponentModel.Design.ArrayEditor
    {
        public MyCollectionEditor(Type type)
            : base(type)
        {
        }

        protected override CollectionForm CreateCollectionForm()
        {
            Form form = base.CreateCollectionForm();
            form.Text = "Here you can put your User Friendly Display Text";
            return form as CollectionForm;
        }
    }

    // Example object
    public class MyObject
    {

        // Following Reza Aghaei's comment I added overridden method
        public override string ToString()
        {
            return "Friendly name";
        }

        [DisplayName("Friendly property name")]
        public string Text
        {
            get;
            set;
        }
    }
}

并且请提供更多信息,说明您正在尝试做什么。