PropertyGrid 属性表格控件

PropertyGrid 属性表格控件

PropertyGrid 是一种可以展示和编辑对象属性的控件。我们可以预先定义一个类,类中包含属性,然后将该类的实例绑定到 PropertyGrid 上,即可在界面中以表格形式查看和修改属性值。

定义实体类

public class AppSettings
{
    private bool saveOnClose = true;
    private string greetingText = "欢迎使用应用程序!";
    private int itemsInMRU = 4;
    private int maxRepeatRate = 10;
    private bool settingsChanged = false;
    private string appVersion = "1.0";
    private Font font;

    public bool SaveOnClose
    {
        get { return saveOnClose; }
        set { saveOnClose = value; }
    }

    public string GreetingText
    {
        get { return greetingText; }
        set { greetingText = value; }
    }

    public int MaxRepeatRate
    {
        get { return maxRepeatRate; }
        set { maxRepeatRate = value; }
    }

    public int ItemsInMRUList
    {
        get { return itemsInMRU; }
        set { itemsInMRU = value; }
    }

    public bool SettingsChanged
    {
        get { return settingsChanged; }
        set { settingsChanged = value; }
    }

    public string AppVersion
    {
        get { return appVersion; }
        set { appVersion = value; }
    }

    public Font Font
    {
        get { return font; }
        set { font = value; }
    }
}Code language: JavaScript (javascript)

绑定对象到 PropertyGrid

AppSettings appSetting = new AppSettings();

private void Form1_Load(object sender, EventArgs e)
{
    this.propertyGrid1.SelectedObject = appSetting;
}Code language: JavaScript (javascript)

运行后,PropertyGrid 会自动读取 AppSettings 类的所有公共属性,以分类表格的形式展示出来,用户可以直接在界面上修改各个属性的值。

常用属性

属性说明
SelectedObject要显示和编辑的对象
SelectedObjects多个选中对象(支持同时编辑多个对象)
PropertySort属性排序方式(NoSort、Alphabetical、Categorized、CategorizedAlphabetical)
HelpVisible是否显示底部帮助说明区域
ToolbarVisible是否显示工具栏
LargeButtons工具栏按钮是否大尺寸
BackColor背景色
LineColor分隔线颜色
HelpBackColor帮助区域背景色
this.propertyGrid1.PropertySort = PropertySort.CategorizedAlphabetical;
this.propertyGrid1.HelpVisible = true;
this.propertyGrid1.ToolbarVisible = true;Code language: JavaScript (javascript)

使用特性(Attribute)增强显示

通过为属性添加特性,可以控制 PropertyGrid 中的显示名称、描述、分类和只读状态:

using System.ComponentModel;

public class AppSettings
{
    private bool saveOnClose = true;

    [Category("常规")]
    [Description("关闭时是否自动保存")]
    [DisplayName("关闭时保存")]
    public bool SaveOnClose
    {
        get { return saveOnClose; }
        set { saveOnClose = value; }
    }

    [Category("常规")]
    [Description("欢迎文本")]
    [ReadOnly(true)]
    public string GreetingText { get; set; } = "欢迎使用应用程序!";

    [Category("性能")]
    [Description("最大重复率")]
    public int MaxRepeatRate { get; set; } = 10;

    [Browsable(false)]  // 在 PropertyGrid 中隐藏该属性
    public bool SettingsChanged { get; set; }
}Code language: JavaScript (javascript)
特性说明
Category属性分组
Description底部帮助区域的描述文本
DisplayName显示名称
ReadOnly是否只读
Browsable是否在 PropertyGrid 中可见
DefaultValue默认值标记

常用事件

事件说明
PropertyValueChanged属性值被用户修改时触发
PropertyTabChanged属性选项卡切换时触发
SelectedObjectsChanged选中对象改变时触发
private void propertyGrid1_PropertyValueChanged(object sender, PropertyValueChangedEventArgs e)
{
    string propertyName = e.ChangedItem.Label;
    object newValue = e.ChangedItem.Value;
    MessageBox.Show($"属性 {propertyName} 已修改为 {newValue}");
}Code language: JavaScript (javascript)

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注