PropertyGrid--基本功能
一.引言
PropertyGrid是Visual Studio中新增的控件,顾名思义,其主要是用来显示和设置某一(或多个)对象的属性值的。其具体的实现主要是基于.Net中强大的反射机制。PropertyGrid功能十分的强大,本人能力有限,集MSDN及丰富的网络资源,略窥一斑,虽晓之甚少,亦不敢掩藏,呈之于文,抛砖引玉。
二.PropertyGrid基本功能
1. PropertyGrid简介
使用PropertyGrid非常简单,这得感谢微软的技术大牛们,控件已经封装的非常完美,拿来就用,以下是MSDN中对PropertyGrid的介绍:
(1)要使用PropertyGrid,应在父控件上创建 PropertyGrid 类的新实例,并将 SelectedObject设置成要为其显示属性的对象。
(2)网格中显示的信息是分配对象时的属性快照。 如果在运行时从代码中更改了由 SelectedObject 指定的对象的属性值,则一直要到在网格中执行了某个操作而使网格刷新时,才会显示新值。
(3)PropertyGrid中的属性选项卡作为按钮显示在 PropertyGrid 顶部的工具栏上,并且可以在 PropertyTabScope 中定义的范围内变化。可以使用 LargeButtons 属性公开用按钮而不是该默认值较小。 大按钮为 32 x 32 像素,而不是标准的 16 x 16 像素。 大小按钮和属性列出 expander 图标 (加号图标) 基于一组系统的 DPI 调整大小,当 app.config 文件包含以下项时:
<appSettings>
<add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />
</appSettings>
(4)PropertyGrid 控件最初并不在开发环境的工具箱中提供。 可以将属性网格添加到工具箱中,以便将 PropertyGrid 拖到窗体中。 您还可以通过在源代码中添加相应的代码来定义 PropertyGrid 的实例。
(5)默认情况下,SelectedObject 的所有公共属性将会在 PropertyGrid 中显示。 您可以通过使用 BrowsableAttribute 修饰某属性并将其值设置为 false 来隐藏该属性,以使其不在 PropertyGrid 控件中显示。 您可以提供具有 CategoryAttribute 的一个类别来指定在其中显示属性的类别。 您可以使用 DescriptionAttribute 为在 PropertyGrid 控件底部显示的属性提供说明性文字。
这几段文字基本说明了PropertyGrid的用法,很简单,只要new 一个PropertyGrid对象(或者直接从工具箱中拖),设置其几个属性(大小,位置),需要显示某个对象的属性时,只需将该对象赋给PropertyGrid的SelectedObject属性即可。
如果对象的属性通过其他的方式(不是通过PropertyGrid)改变了,只需要调用PropertyGrid的Refresh方法,即可更新。
PropertyGrid中的属性选项卡作为按钮显示在 PropertyGrid 顶部的工具栏上,通过LargeButtons 属性可以设置按钮使用大图标或小图标。
小图标效果:
大图标效果:
2.使用方法
下面举个具体的例子进行说明:
假如现在我们要自定义一个控件,我们希望我们的控件的属性能显示在属性框中。
①新建Windows工程,在Form对话框中拖入一个PropertyGrid控件。
②添加我们的控件类,当然这个控件没什么用,只是用来举例说明:
![](/assets/blank.gif)
![](/assets/blank.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace TestPropertyGrid 11 { 12 public class SimpleControl : System.Windows.Forms.Control 13 { 14 private int _prop; 15 16 public int Prop 17 { 18 get { return _prop; } 19 set { this._prop = value; } 20 } 21 22 23 public SimpleControl() 24 { 25 26 } 27 } 28 }
View Code
③在Form1中添加我们的控件,并将其赋给propertyGrid1.SelectedObject属性:
![](/assets/blank.gif)
![](/assets/blank.gif)
1 public partial class Form1 : Form 2 { 3 SimpleControl _simpleControl;//自定义控件 4 5 public Form1() 6 { 7 InitializeComponent(); 8 9 //通过PropertyGrid控件显示自定义控件属性 10 _simpleControl = new SimpleControl(); 11 this.propertyGrid1.SelectedObject = _simpleControl; 12 } 13 }
View Code
④运行程序,在属性框的最下面,可以找到一个杂项的节点,显示我们自定义控件中的属性。
属性显示在杂项中,不好看,且功能不明确,这里需要设置属性的特性(Atrribute)来控制他在属性框中的展现。这里需要使用上面所说到的BrowsableAttribute, CategoryAttribute,DescriptionAttribute三个特性。使用这些特性的时候,注意要引入命名空间 System.ComponentModel。
BrowsableAttribute:所修饰的属性是否在 PropertyGrid 控件中显示。
CategoryAttribute:所修饰的属性在PropertyGrid 控件中分类(即杂项)
DescriptionAttribute:在 PropertyGrid 控件底部显示的说明性文字
在我们自定义控件中的属性加上这些特性:
![](/assets/blank.gif)
![](/assets/blank.gif)
1 [Browsable(true), Category("布局"),Description("just a test")] 2 public int Prop 3 { 4 get { return _prop; } 5 set { this._prop = value; } 6 }
View Code
运行效果如下,属性出现在布局类别中,选中该项,下面会出现Description特性中描述的文字。
3.对象集合的属性显示
接下来我们再定义一个自定义控件SimpleControl2,该控件和SimpleControl一样有个Prop属性,但多一个Prop2属性:
![](/assets/blank.gif)
![](/assets/blank.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.ComponentModel; 6 7 namespace TestPropertyGrid 8 { 9 public class SimpleControl2 : System.Windows.Forms.Control 10 { 11 private int _prop; 12 private int _prop2; 13 14 [Browsable(true), Category("布局"), Description("just a test")] 15 public int Prop 16 { 17 get { return _prop; } 18 set { this._prop = value; } 19 } 20 21 [Browsable(true), Category("布局"), Description("just a test")] 22 public int Prop2 23 { 24 get { return _prop2; } 25 set { this._prop2 = value; } 26 } 27 28 29 public SimpleControl2() 30 { 31 _prop = 1; 32 } 33 }
View Code
在Form1中添加一个SimpleControl2控件和Control集合:
![](/assets/blank.gif)
![](/assets/blank.gif)
1 public partial class Form1 : Form 2 { 3 SimpleControl _simpleControl; //控件1 4 SimpleControl2 _simpleControl2; //控件2 5 Control[] _controls; //集合 6 7 public Form1() 8 { 9 InitializeComponent(); 10 11 _simpleControl = new SimpleControl(); 12 _simpleControl2 = new SimpleControl2(); 13 _controls = new Control[] { _simpleControl, _simpleControl2 }; 14 15 this.propertyGrid1.SelectedObject = _controls; 16 } 17 }
View Code
当我们将_controls集合赋给SelectedObject时,效果如下:
PropertyGrid将集合中的元素分别列举出来([0],[1]),而且属性的分类排序和字母排序好像失效,但是属性是全部显示的。
下面将this.propertyGrid1.SelectedObject = _controls;替换为this.propertyGrid1.SelectedObjects = _controls;效果如下:
PropertyGrid只会显示集合中元素所共有的属性,(如上例中有Prop而没有Prop2),且如果共有属性的值不同,值是不会显示的,(如Prop的初始值不同,故其值不显示),但设置的时候,会将所有元素的属性设置为同一值。
转载于:https://www.cnblogs.com/yongyu/p/4227391.html
PropertyGrid--基本功能相关推荐
- PropertyGrid自定义控件
PropertyGrid是一个很强大的控件,使用该控件做属性设置面板的一个好处就是你只需要专注于代码而无需关注UI的呈现,PropertyGrid会默认根据变量类型选择合适的控件显示.但是这也带来了一 ...
- [转载]C# PropertyGrid控件应用心得
最近碰到关于PropertyGrid控件,以前没有使用过,在网上找了一篇不错的文章,转来分享一下!建议查看原文章! 原文链接:http://blog.csdn.net/luyifeiniu/artic ...
- 动态可订制属性的 PropertyGrid(转载)
参考:http://bbs.csdn.net/topics/380095345 参考:http://bbs.csdn.net/topics/390331033 转自:http://blog.csdn. ...
- PropertyGrid控件 分类(Category)及属性(Property)排序
最近在做表单设计器,设计器上的控件都是我们自己封装的,但每个属性类别里的属性是按照属性的拼音排序的,现在想按照PropertyIndex标识进行排序(PropertyIndex的后三位是用来标识编辑器 ...
- 一款开源且功能强大的C#甘特图控件.NET Winforms Gantt Chart Control
甘特图在项目管理中非常重要,甘特图的思想比较简单,即以图示的方式通过活动列表和时间刻度形象地表示出任何特定项目的活动顺序与持续时间.它直观地表明任务计划在什么时候进行,及实际进展与计划要求的对比.管理 ...
- Winform PropertyGrid使用方法
原文连接 先来转一个基础的,源自msdn http://www.microsoft.com/taiwan/msdn/library/2002/Jul-2002/article/usingpropgri ...
- 用 PropertyGrid 实现 Visual Studio 2005 样式的属性窗口
在 Visual Studio 里,有一个很好用的东西,就是属性窗口 (Properties Window),如下图所示.属性窗口列出了一个对象所有可见的属性,你可以浏览和修改属性,非常方便. < ...
- c#中PropertyGrid 控件简介
PropertyGrid 控件简介 如果您使用过 Microsoft® Visual Basic® 或 Microsoft Visual Studio .NET,那么您一定使用过属性浏览器来浏览.查看 ...
- PropertyGrid使用方法
原文连接 先来转一个基础的,源自msdn http://www.microsoft.com/taiwan/msdn/library/2002/Jul-2002/article/usingpropgri ...
最新文章
- 这组动画完美演绎了一个程序员从接手新项目到交货的复杂心情
- jQuery核心函数的使用总结
- vue响应的res.data和res.data.data
- 讲一讲应用服务的新鲜事儿
- upload file more than 4MB
- JS面向对象的程序设计之创建对象_工厂模式,构造函数模式,原型模式-1
- 吴恩达深度学习4.4笔记_Convolutional Neural Networks_人脸识别和神经风格转换
- 一步步学习SPD2010--第二章节--处理SP网站(9)---- 关键点
- java8 json转xml,在Java中将JSON转换为XML
- python 服务端渲染_详解React 服务端渲染方案完美的解决方案
- 二叉树非递归遍历(先序、中序、后序)(C++)
- 计算机怎么看显卡内存容量,Win10系统显卡显存大小怎样查看?Win10查看显存大小的两种方法...
- excel表格如何换行
- 避坑:关于两个HC-05主从一体蓝牙模块互连,连不上问题
- uniapp引入支付宝原生扫码插件步骤详解
- [附源码]JAVA+ssm计算机毕业设计房屋租赁管理系统(程序+Lw)
- notepad中html自动补齐和标签,在Notepad ++中显示不匹配的html标签(Show unmatched html tags in Notepad++)...
- 星星之火OIer:手机网络
- disconf在windows环境下部署调试
- sql 数据与程序的物理独立性和逻辑独立性