PropertyGrid 控件简介

.NET 框架 PropertyGrid 控件是 Visual Studio .NET 属性浏览器的核心。PropertyGrid 控件显示对象或类型的属性,并主要通过使用反射来检索项目的属性。当我们创建了一个类编译之后就生成了类的Metadata,元数据。PropertyGrid 就是使用反射来展示和修改类的公共属性的,就是public标识的属性。但凡成熟的软件都是使用配置来满足不同场景或者客户的需求,就是使用参数开关的形式。PropertyGrid 代表了主流的配置界面,对于实施培训的技术支持人员比较人性化。现在就来分享一下使用心得。

SelectedObject

PropertyGrid 的方法SelectedObject是获取或设置在网格中显示属性的对象。是使用PropertyGrid显示对象的最重要方法。

实际用例截图

使用的要点

  • 1)属性值如果是枚举类型,则设置改属性时自动生成下拉框和填充选择值。
  • 2)属性可自定义分类
  • 3)属性可注释和说明
  • 4)属性设置可自定义属性类型
  • 5)如果需要隐藏属性不在PropertyGrid里显示出来,可以在类的属性上标记[Browsable(false)] 
 private string _动态内容;
[Description(@"格式:{PropertyName[(Start[,Length])]}[&Blank[(Length)]]&{PropertyName[(Start[,Length])]}
PropertyName:动态属性里的选项
Start:动态属性对应内容的开始位置
Length:截取内容的长度
Blank:空格
Length:空格的个数
&:为分隔符
设置此内容的时候,请务必小心,设置时系统不检测其值的合法性,在执行的时候可能会报错"), Category("表达式")]
[XmlAttribute("动态内容")] public string 动态内容
{
get { return _动态内容; }
set { _动态内容 = value; NotifyPropertyChanged("动态内容"); }
}

要更改某些属性的显示方式,您可以对这些属性应用不同的特性。特性是用于为类型、字段、方法和属性等编程元素添加批注的声明标记,在运行时可以使用反射对其进行检索。下面列出了其中的一部分:

DescriptionAttribute - 设置显示在属性下方说明帮助窗格中的属性文本。这是一种为活动属性(即具有焦点的属性)提供帮助文本的有效方法。

CategoryAttribute - 设置属性在网格中所属的类别。当您需要将属性按类别名称分组时,此特性非常有用。如果没有为属性指定类别,该属性将被分配给杂项 类别。可以将此特性应用于所有属性。

BrowsableAttribute –  表示是否在网格中显示属性。此特性可用于在网格中隐藏属性。默认情况下,公共属性始终显示在网格中。

ReadOnlyAttribute –  表示属性是否为只读。此特性可用于禁止在网格中编辑属性。默认情况下,带有 get 和 set 访问函数的公共属性在网格中是可以编辑的。

DefaultValueAttribute –  表示属性的默认值。如果希望为属性提供默认值,然后确定该属性值是否与默认值相同,则可使用此特性。可以将此特性应用于所有属性。

DefaultPropertyAttribute –  表示类的默认属性。在网格中选择某个类时,将首先突出显示该类的默认属性。

  

自定义PropertyGrid属性 请参考文章:http://blog.csdn.net/jjhua/article/details/23100143.

PropertyGrid显示英文字符为中文

PropertyGrid显示英文字符为中文,需要自定义属性和实现一个自定义的PropertyDescriptor子类,还有一个实现了"ICustomTypeDescriptor"接口的类。

namespace GDIPrinterDriver
{
public delegate void PropertyChanged(object Value);
/// <summary>
/// 主要是实现中文化属性显示
/// </summary>
public class PropertyBase : ICustomTypeDescriptor
{
AttributeCollection ICustomTypeDescriptor.GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}
string ICustomTypeDescriptor.GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}
string ICustomTypeDescriptor.GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}
TypeConverter ICustomTypeDescriptor.GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
{
return null;
}
object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
return ((ICustomTypeDescriptor)this).GetProperties(new Attribute[0]);
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
{
ArrayList props = new ArrayList();
Type thisType = this.GetType();
PropertyInfo[] pis = thisType.GetProperties();
foreach (PropertyInfo p in pis)
{
if (p.DeclaringType == thisType || p.PropertyType.ToString() == "System.Drawing.Color")
{
//判断属性是否显示
BrowsableAttribute Browsable = (BrowsableAttribute)Attribute.GetCustomAttribute(p, typeof(BrowsableAttribute));
if (Browsable != null)
{
if (Browsable.Browsable == true || p.PropertyType.ToString() == "System.Drawing.Color")
{
PropertyStub psd = new PropertyStub(p, attributes);
props.Add(psd);
}
}
else
{
PropertyStub psd = new PropertyStub(p, attributes);
props.Add(psd);
}
}
}
PropertyDescriptor[] propArray = (PropertyDescriptor[])props.ToArray(typeof(PropertyDescriptor));
return new PropertyDescriptorCollection(propArray);
}
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
} /// <summary>
/// 自定义属性拦截器
/// </summary>
public class PropertyStub : PropertyDescriptor
{
PropertyInfo info;
public PropertyStub(PropertyInfo propertyInfo, Attribute[] attrs)
: base(propertyInfo.Name, attrs)
{
info = propertyInfo;
}
public override Type ComponentType
{
get { return info.ReflectedType; }
}
public override bool IsReadOnly
{
get { return info.CanWrite == false; }
}
public override Type PropertyType
{
get { return info.PropertyType; }
}
public override bool CanResetValue(object component)
{
return false;
}
public override object GetValue(object component)
{
try
{
return info.GetValue(component, null);
}
catch
{
return null;
}
}
public override void ResetValue(object component)
{
}
public override void SetValue(object component, object value)
{
info.SetValue(component, value, null);
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
//通过重载下面这个属性,可以将属性在PropertyGrid中的显示设置成中文
public override string DisplayName
{
get
{
if (info != null)
{
ChnPropertyAttribute uicontrolattibute = (ChnPropertyAttribute)Attribute.GetCustomAttribute(info, typeof(ChnPropertyAttribute));
if (uicontrolattibute != null)
return uicontrolattibute.PropertyName;
else
{
return info.Name;
}
}
else
return "";
}
} public override string Description
{
get
{
if (info != null)
{
ChnPropertyAttribute uicontrolattibute = (ChnPropertyAttribute)Attribute.GetCustomAttribute(info, typeof(ChnPropertyAttribute));
if (uicontrolattibute != null)
return uicontrolattibute.PropertyDescription;
}
return string.Empty;
}
}
}
}

然后在PropertyGrid需要维护的类上作自定义属性的标记。

public class BarcodeElementNode : PropertyBase, IElementNodeData, INotifyPropertyChanged

    {
private Point location;
[ChnProperty("位置", "节点元素的在模板里的位置的坐标,鼠标选中节点即可以移动位置。")]
[Category("通用属性")]
public Point Location
{
get { return location; }
set { location = value; }
}
}

  

/// <summary>
/// 中文方式自定义属性标识
/// </summary>
public class ChnPropertyAttribute : Attribute
{
private string _PropertyName;
private string _PropertyDescription; public ChnPropertyAttribute(string Name, string Description)
{
_PropertyName = Name;
_PropertyDescription = Description;
}
public ChnPropertyAttribute(string Name)
{
_PropertyName = Name;
_PropertyDescription = "";
}
public string PropertyName
{
get { return _PropertyName; }
}
public string PropertyDescription
{
get { return _PropertyDescription; }
}
}

 

  

好了。收工,不写点啥心慌。但是写好需要花很多时间。只是坚持一下吧

System.Windows.Forms.PropertyGrid的使用的更多相关文章

  1. WPF中实例化Com组件,调用组件的方法时报System.Windows.Forms.AxHost+InvalidActiveXStateException的异常

    WPF中实例化Com组件,调用组件的方法时报System.Windows.Forms.AxHost+InvalidActiveXStateException的异常 在wpf中封装Com组件时,调用组件 ...

  2. 用户控件的设计要点 System.Windows.Forms.UserControl

    用户控件的设计要点 最近的项目中有一个瀑布图(彩图)的功能,就是把空间和时间上的点量值以图的形式呈现出来,如下图: X坐标为空间,水平方向的一个像素代表一个空间单位(例如50米) Y坐标为时间,垂直方 ...

  3. System.Windows.Forms.Timer与System.Timers.Timer的区别(zz)

    .NET Framework里面提供了三种Timer: System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer VS ...

  4. 找不到命名空间命名空间:System.Windows.Forms

    System.Windows.Forms在system.windows.forms.dll中.需要添加引用.在解决方案资源管理器中的引用上单击右键,选择添加引用.找到System.windows.fo ...

  5. System.Windows.Forms.AxHost.InvalidActiveXStateException”类型的异常在 ESRI.ArcGIS.AxControls.dll 中发生,但未在用户代码中进行处理

    private void CopyAndOverwriteMap() { //IObjectCopy接口变量申明 IObjectCopy objectCopy = new ObjectCopyClas ...

  6. “FormCRUD.csProj.FormMain.Name”隐藏了继承的成员“System.Windows.Forms.Control.Name”。如果是有意隐藏,请使用关键字 new。

    一旦运行就显示:“FormCRUD.csProj.FormMain.Name”隐藏了继承的成员“System.Windows.Forms.Control.Name”.如果是有意隐藏,请使用关键字 ne ...

  7. .net chart(图表)控件的使用-System.Windows.Forms.DataVisualization.dll

    这个案例指在介绍微软这套免费又功能强大的图表控件Microsoft Chart Controls for Microsoft .NET Framework 3.5,通过它,可让您的项目及报表,轻松套用 ...

  8. System.Windows.Forms.Timer反编译学习

    using System; using System.ComponentModel; using System.Globalization; using System.Runtime; using S ...

  9. System.Windows.Forms.Timer

    一.主要属性.方法和事件 Windows 窗体 Timer 是定期引发事件的组件.该组件是为 Windows 窗体环境设计的. 时间间隔的长度由 Interval 属性定义,其值以毫秒为单位.若启用了 ...

随机推荐

  1. 最短路算法之Dijkstra算法通俗解释

    Dijkstra算法 说明:求解从起点到任意点的最短距离,注意该算法应用于没有负边的图. 来,看图. 用邻接矩阵表示 int[][] m = { {0, 0, 0, 0, 0, 0}, {0, 0, ...

  2. Ajax中的JSON格式与php传输过程的浅析

    在Ajax中的JSON格式与php传输过程中有哪些要注意的小地方呢? 先来看一下简单通用的JSON与php传输数据的代码 HTML文件: <input type="button&quo ...

  3. 《iOS Human Interface Guidelines》——Multitasking

    多任务处理 多任务处理让人们在屏幕上(以及合适的iPad模式)查看多个app,而且在近期使用的app中高速地切换. 在iOS 9中.人们能够使用多任务处理UI(例如以下所看到的)来选择一个近期使用的a ...

  4. MapReduce编程(一) Intellij Idea配置MapReduce编程环境

    介绍怎样在Intellij Idea中通过创建mavenproject配置MapReduce的编程环境. 一.软件环境 我使用的软件版本号例如以下: Intellij Idea 2017.1 Mave ...

  5. google C++编程风格指南之头文件的包括顺序

    google C++编程风格对头文件的包括顺序作出例如以下指示: (1)为了加强可读性和避免隐含依赖,应使用以下的顺序:C标准库.C++标准库.其他库的头文件.你自己project的头文件.只是这里最 ...

  6. python的unittest測试框架的扩展浅谈

    非常多时候測试框架须要依据測试数据来自己主动生成測试用例脚本,比方接口測试,通过不同參数构建组合去请求接口,然后验证返回结果.假设这样能通过配置excel数据来驱动測试.而不用去写一大堆的測试用例脚本 ...

  7. 《Java NIO (中文版)》【PDF】下载

    <Java NIO (中文版)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230062530 NIO (中文版)>[PDF]& ...

  8. 随笔:JavaScript函数中的对象----arguments

    关于arguments 调用函数时,如果需要传参,其实参数就是一个数组,在函数体的内置对象arguments可以访问这个数组,如: arguments[0]:第一个参数 arguments[1]:第二 ...

  9. 【MySQL】查看字符集对应的校对规则show collation like 'gbk%';

  10. 视觉SLAM的数学表达

    相机是在某些时刻采集数据的,所以只关心这些时刻的位置和地图. 就把这一段时间的运动变成了李三时刻 t=1,2,...K当中发生的事情. 在这些事可,x表示机器自身的位置. x1,x2,x3,x4... ...