Winfrom PropertyGrid实现自定义属性
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Design; using System.Collections.Specialized; namespace UserControl_Collection { public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } List<int> _Counts = new List<int>(); [Browsable(true)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] [Description("LED 數量集合"), Category("LED Description")] public List<int> Counts { get { return _Counts; } set { _Counts = value; } } /// <summary> /// LED 文字說明 /// </summary> //[Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design", typeof(UITypeEditor))] //[Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design", typeof(UITypeEditor))] //[DefaultValue(typeof(string), "")] private List<string> _LEDDescriptions = new List<string>(); [Browsable(true)] [System.ComponentModel.Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design","System.Drawing.Design.UITypeEditor, System.Drawing")] [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] [Description("LED 說明文字")] [Category("LED Description")] public List<string> LEDDescriptions { get { return _LEDDescriptions; } set { _LEDDescriptions = value; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.UI.Design; using System.ComponentModel; using System.ComponentModel.Design; using System.Design; using System.Drawing.Design; using System.Windows.Forms; using System.Web.UI.WebControls; using System.Windows.Forms.Design; using System.Drawing; namespace MyTestProj { public class EditorControl : WebControl { string strTxtEditor; [Category("编辑器")] [Description("下拉多行文本编辑器")] [Editor("System.ComponentModel.Design.MultilineStringEditor,System.Design", typeof(UITypeEditor))] public string TxtEditor { get { return strTxtEditor; } set { strTxtEditor = value; } } Color cColorEditor; [Category("编辑器")] [Description("颜色编辑器")] [Editor("System.ComponentModel.Design.ColorEditor,System.Design", typeof(UITypeEditor))] public Color ColorEditor { get { return cColorEditor; } set { cColorEditor = value; } } string strFileName; [Category("编辑器")] [Description("文件选择编辑器")] [Editor(typeof(FileNameEditor), typeof(UITypeEditor))] public string FileName { get { return strFileName; } set { strFileName = value; } } string strFolderNameEditor; [Category("编辑器")] [Description("目录选择编辑器")] [Editor(typeof(FolderNameEditor), typeof(UITypeEditor))] public string FolderNameEditor { get { return strFolderNameEditor; } set { strFolderNameEditor = value; } } string strConnectionStringEditor; [Category("编辑器")] [Description("连接字符串编辑器")] [Editor(typeof(System.Web.UI.Design.ConnectionStringEditor), typeof(UITypeEditor))] public string ConnectionStringEditor { get { return strConnectionStringEditor; } set { strConnectionStringEditor = value; } } string strExpressionsCollectionEditor; [Category("编辑器")] [Description("编辑表达式绑定集合的编辑器")] [Editor(typeof(System.Web.UI.Design.ExpressionsCollectionEditor), typeof(UITypeEditor))] public string ExpressionsCollectionEditor { get { return strExpressionsCollectionEditor; } set { strExpressionsCollectionEditor = value; } } string strUserControlFileEditor; [Category("编辑器")] [Description("用户控件(ascx)对话框编辑器")] [Editor(typeof(System.Web.UI.Design.UserControlFileEditor), typeof(UITypeEditor))] public string UserControlFileEditor { get { return strUserControlFileEditor; } set { strUserControlFileEditor = value; } } } }
System.Windows.Forms 命名空间中的有
Bindable
这个特性表示属性是否可以绑定一个有效数据源。通常使用布尔值进行设置。例如:Bindable(true)。如果使用值true标记属性,表示该属性可以绑定一个有效数据源,且应引发该属性的属性更改通知。
Browsable
指定属性是否应该在属性浏览器中显示,使用布尔值设置。通常情况下,功用属性和那些希望在属性浏览器中显示的属性被设置为 Browsable(true),只读属性和那些不希望在属性浏览器中见到的属性被设置为 Browsable(false)。
Category
指定属性在属性浏览器中进行分组显示的类别。该设计时特性帮助可视化编辑器将属性进行逻辑分组。通常分为:外观(Appearance)、行为(Behavior)、布局(Layout)、数据(Data)、操作(Action)、键盘(Key)和鼠标(Mouse)等。除此之外,还可以自定义分组类别。
Description
指定显示在属性浏览器下方,属性的文字说明。
DesignerSerializationVisibility
指定属性是否以及如何在代码中序列化,其值为DesignerSerializationVisibility的枚举值。存在3种设置方式:DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)指定序列化程序不应该序列化属性值;DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)指定应该允许序列化程序序列化属性的值;
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)指定序列化程序应该序列化属性的内容,而不是属性本身。此字段为只读。.Visible为其默认值。
NotifyParentProperty
指示当此特性应用到的属性的值被修改时将通知其父属性。换言之,如果属性的父属性应该在该属性值改改时接到通知,则向该属性应用NotifyParentProperty特性。通常使用布尔值进行设置。
System.Web.UI命名空间的有(Asp.net服务器控件专用)
ParseChildren
使用该特性指示当在页面上以声明方式使用控件时,嵌套在服务器控件标记内的XML元素是应该视为属性还是应视为子控件。通常情况下,包含两种声明方式:①ParseChildren(true)表示将子XML元素作为服务器控件的子控件分析;②ParseChildren(bool childrenasProperty, string defaultProperty),其中childrenasProperty和方式①中的布尔值参数意义相同,defaultProperty定义默认情况下将子控件分析为服务器控件的集合属性。
PersistChildren
该特性指示设计时是否应将服务器控件的子控件作为内部嵌套控件保持。如果该特性为PersistChildren(true),则将服务器控件的子控件作为嵌套服务器控件标记保持。如果为PersistChildren(false),则将该控件的属性作为嵌套元素保持。
PersistenceMode
指定如何将服务器控件属性或事件保持到ASP.NET页面的元数据属性。共存在4种枚举设置方式:①PersistenceMode(PersistenceMode.Attribute),指定属性或事件保持为特性;②PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty),指定属性作为服务器控件的唯一内部文本而属性值是HTML编码的,只能对字符串作这种指定;③PersistenceMode(PersistenceMode.InnerDefaultProperty),指定属性在服务器控件中保持为内部文本,还指示将该属性定义为元素的默认属性,只能指定一个属性为默认属性;④PersistenceMode(PersistenceMode.InnerProperty),指定属性在服务器控件中保持为嵌套标记,这通常用于复杂对象,它们具有自己的持久性属性。
DefaultProperty
指定服务器控件的默认属性,例如[DefaultProperty("MyProperty")]。
TypeConverter
指定用做此特性所绑定到的对象的转换器的类型。用于转换的类必须从TypeConverter继承。使用ConverterTypeName属性来获取为该特性所绑定到的对象提供数据转换的类名。
浙公网安备 33010602011771号