C# PropertyGrid的使用

作用:使用PropertyGrid控件可以与属性进行连接,自动生成属性表格。

上代码:

//创建一个属性类
public class Employee
{
    [Category("Info"),Browsable(false)]
    public Guid ID { get; set; }
    [Category("Info"),DisplayName("姓名")]
    public string FirstName { get; set; }
    [Category("Info"),Description("这是一段描述")]
    public string LastName { get; set; }
    [Category("Info"),Range(1,99)]
    public int Age { get; set; }
    [Category("Job")]
    public Double Salary { get; set; }
    [Category("Job")]
    public string Position { get; set; }
    [Category("Job")]
    public string Department { get; set; }
}

与PropertyGrid控件进行连接

var employee = new Employee
{
    ID = Guid.NewGuid(),
    FirstName = "Tom",
    LastName = "Riddle",
    Age = 25,
    Salary = 10000,
    Position = "Software Engineer",
    Department = "Development"
};

propertyGrid1.SelectedObject = employee;

效果:

 

posted @ 2022-09-28 10:53  Echo_Lee0823  阅读(787)  评论(0编辑  收藏  举报