属性网格集合成员名称

时间:2017-03-09 08:39:59

标签: c# winforms propertygrid

有没有办法编辑Collection编辑器以及显示某些值。我的收藏品的成员在向用户显示时我想要一个更加用户友好的名称而不是解决方案名称加上作为名称的类显示。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

你说" ...解决方案名称加上班级名称......"并使用" 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;
        }
    }
}

请提供更多相关信息。