using System;
using System.Windows.Forms;
public class PropertyGridExample : Form
{
private PropertyGrid propertyGrid1;
private MyObject myObject;
public PropertyGridExample()
{
// 初始化 PropertyGrid 控件
propertyGrid1 = new PropertyGrid();
propertyGrid1.Dock = DockStyle.Fill;
this.Controls.Add(propertyGrid1);
// 创建一个自定义对象并将其绑定到 PropertyGrid
myObject = new MyObject();
propertyGrid1.SelectedObject = myObject;
}
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new PropertyGridExample());
}
}
// 自定义类,用于展示在 PropertyGrid 中
public class MyObject
{
// 属性 1:字符串类型
public string Name { get; set; } = "DefaultName";
// 属性 2:整数类型
public int Age { get; set; } = 30;
// 属性 3:布尔类型
public bool IsActive { get; set; } = true;
// 属性 4:枚举类型
public Colors FavoriteColor { get; set; } = Colors.Blue;
// 枚举类型定义
public enum Colors
{
Red,
Green,
Blue,
Yellow
}
}
PropertyGrid 控件:这是一个 Windows Forms 控件,允许用户查看和编辑对象的属性。它会自动显示对象的所有公共属性,并提供相应的编辑界面。
MyObject 类:这是一个自定义类,包含了一些不同类型的属性(如字符串、整数、布尔值和枚举)。这些属性会被 PropertyGrid 显示和编辑。
Main 方法:这是应用程序的入口点,创建并运行 PropertyGridExample 窗体。
属性绑定:通过 propertyGrid1.SelectedObject = myObject; 将自定义对象 myObject 绑定到 PropertyGrid,从而可以在界面上查看和修改该对象的属性。
这个示例展示了如何使用 PropertyGrid 控件来查看和编辑对象的属性。
上一篇:c# 遍历枚举
下一篇:c# 对象转换json
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站