c# PropertyGrid 使用案例注意事项

文章参考链接:C# PropertyGrid使用案例详解_C#教程_脚本之家 (jb51.net)

1. 只有public的property能显示出来,可以通过BrowsableAttribute来控制是否显示,通过CategoryAttribute设置分类,通过DescriptionAttribute设置描述,Attribute可以加在Class上,也可以加在属性上,属性上的Attribute优先级更高;

2. enum会自动使用列表框表示;

3. 自带输入有效性检查,如int类型输入double数值,会弹出提示对话框;

4. 基本类型Array:增加删除只能通过弹出的集合编辑器,修改可以直接展开,值为null时,可以通过集合编辑器创建;

5. 基本类型List:增删改都只能通过集合编辑器,值为null时,能打开集合编辑器,但不能保存结果,所以必须初始化;-----使用时必须初始化,才可以使用

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            TestPram testPram = new TestPram();
              propertyGrid1.SelectedObject = testPram;
        }
    }
    public class TestPram
    {
        public List<PointXY> Names { get; set; } = new List<PointXY> { new PointXY { X = 2, Y = 3 } };
            
    }
    public class PointXY
    {
        public float X { get; set; }
        public float Y { get; set; }
        
    }

6. ReadOnlyAttribute设置为true时,显示为灰色;对List无效,可以打开集合编辑器,在集合编辑器内可以进行增删改;应用于Array时,不能打开集合编辑器,即不能增删,但可以通过展开的方式修改Array元素;

7. 对象:值显示Object.ToString();Class上加了[TypeConverter(typeof(ExpandableObjectConverter))]之后(也可以加在属性上),才能展开编辑,否则显示为灰色只读;不初始化什么也干不了;

8. 基类类型派生类对象:与普通对象没有区别,[TypeConverter(typeof(ExpandableObjectConverter))]加在基类上即可;

9. 对象Array:增加删除只能通过弹出的集合编辑器,修改可以直接展开,值为null时,可以通过集合编辑器创建;

10. 对象List:增加删除修改只能通过弹出的集合编辑器,值为null时,能打开集合编辑器,但不能保存结果,所以必须初始化;

11. Hashtable和Dictionary能打开集合编辑器,但不能编辑;