PropertyGrid用法,有助于自定义控件的property实现.

    技术2022-05-19  24

    转自:http://blog.163.com/wingstart@126/blog/static/1065920062010927103723477/

     

    拖个PropertyGrid,绑定一个属性类就行了。

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace PropertyGridApp { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { propertyGrid1.SelectedObject = new Go(); } class Go { private string _Hi = "hi"; public string Hi { get { return _Hi; } set { _Hi = Hi; } } } } }

    它能自动识别Go类中的属性,并且自动关联。

    对属性进行分类并加注释:

     class Go { private float _TieMu = 5.5f; private string _Rule = "数子法"; [CategoryAttribute("规则"), DescriptionAttribute("贴目")] public float TieMu { get { return _TieMu; } set { _TieMu = TieMu; } } [CategoryAttribute("规则"), DescriptionAttribute("计算法")] public string Rule { get { return _Rule; } set { _Rule = Rule; } } private int _Black = 0; private int _White = 0; [CategoryAttribute("围棋"), DescriptionAttribute("黑")] public int Black { get { return _Black; } set { _Black = Black; } } [CategoryAttribute("围棋"), DescriptionAttribute("白")] public int White { get { return _White; } set { _White = White; } } }

    使用Color类型可以显示颜色选择下拉框,使用Image类型可以显示图片选择对话框,真强大。

    private Color _BoardColor = Color.Yellow; [CategoryAttribute("围棋"), DescriptionAttribute("棋盘颜色")] public Color BoardColor { get { return _BoardColor; } set { _BoardColor = BoardColor; } } private Image _Background; [CategoryAttribute("围棋"), DescriptionAttribute("棋盘背景")] public Image Background { get { return _Background; } set { _Background = Background; } }

    另外是自定义类型,比如枚举.


    最新回复(0)