C# WinForm PropertyGrid用法
关于C# PropertyGrid的用法没有找到,找到一个C++的用法。
模仿着使用了一下,感觉挺不错,分享一下。
基本用法:
拖个PropertyGrid,绑定一个属性类就行了。
大气象 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->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类中的属性,并且自动关联。
对属性进行分类并加注释:
大气象 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->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类型可以显示图片选择对话框,真强大。
大气象 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->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; } }
另外是自定义类型,比如枚举.
源码:https://files.cnblogs.com/greatverve/PropertyGridApp.rar
参考:
http://blog.csdn.net/xoyojank/archive/2009/07/04/4322167.aspx
分类:
C#与.NET
标签:
C#
, PropertyGrid
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理