版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、.net 控件开发常见的特性总结在.net开发中常常需要使用一些里面的特性描述,我发现对常用的几个知道大概的意思,但是却不甚清楚到底有多少个特性,都如何使用。所以今天查了下msdn,把他们一一总结下来。这些特性(属性)在System.ComponentModel下面,凡是带有Attribute结尾的的类都是可以作为方括号里面使用的。我将它们一一摘录下来,并且写上了自己的理解。AttributeUsageAttribute用法:view plaincopy to clipboardprint?01.AmbientValue(typeof(Color), Empty) 02.Category(Ap
2、pearance) 03.DefaultValue(typeof(Color), White) 04.Description(The color used for painting alert text.) 05.public Color AlertForeColor 06. 07. get 08. 09. if (this.alertForeColorValue = Color.Empty & 10. this.Parent != null) 11. 12. return Parent.ForeColor; 13. 14. 15. return this.alertForeColorValu
3、e; 16. 17. 18. set 19. 20. this.alertForeColorValue = value; 21. 22. AmbientValue(typeof(Color), Empty)Category(Appearance)DefaultValue(typeof(Color), White)Description(The color used for painting alert text.)public Color AlertForeColor get if (this.alertForeColorValue = Color.Empty & this.Parent !=
4、 null) return Parent.ForeColor; return this.alertForeColorValue; set this.alertForeColorValue = value; 大概含义:如果控件上的属性具有环境行为,则必须存在此特性。环境属性向其父级查询它们的值,例如 Control.Font 属性或 Control.BackColor 属性。通常,可视化设器使用 AmbientValueAttribute 属性来决定为属性永久保存的值。这通常是一个使属性从另一个源获取其值的值。周围值的一个示例是 Color.Empty 作为 BackColor 属性的周围值。如
5、果您在窗体上具有一个控件,并且该控件的 BackColor 属性被设置为与该窗体的 BackColor 属性不同的颜色,则您可以通过将该控件的 BackColor 设置为 Color.Empty 来将该控件的 BackColor 属性重置为该窗体的颜色。AttributeUsageAttributeview plaincopy to clipboardprint?01.Category(Data) 02.Description(Indicates the source of data for the control.) 03.RefreshProperties(RefreshPropertie
6、s.Repaint) 04.AttributeProvider(typeof(IListSource) 05.public object DataSource 06. 07. get 08. 09. return this.dataGridView1.DataSource; 10. 11. 12. set 13. 14. this.dataGridView1.DataSource = value; 15. 16. Category(Data)Description(Indicates the source of data for the control.)RefreshProperties(R
7、efreshProperties.Repaint)AttributeProvider(typeof(IListSource)public object DataSource get return this.dataGridView1.DataSource; set this.dataGridView1.DataSource = value; 大概含义:在 .NET Framework 对象模型中,有些情况下会有意将属性类型化为模糊的。例如,将 DataGridView.DataSource 属性类型化为 object。之所以这样,是为了让此属性可以接受多种类型的输入。遗憾的是,这样做并未提供添
8、加元数据以描述属性特性的通用方法。对于需要元数据的类型转换器、UI 类型编辑器和其他服务,.NET Framework 中的每个 DataSource 属性都需要具有相同的元数据。AttributeProviderAttribute 对此情况进行了补救。此属性 (Attribute) 一旦被置于属性 (Property) 上,用于获得属性 (Property) 描述符的 MemberDescriptor.Attributes 集合的属性 (Attribute) 的规则就会有所不同。通常,属性 (Property) 描述符收集本地属性 (Attribute),然后再将其与来自属性 (Proper
9、ty) 类型的属性 (Attribute) 进行合并。在此情况下,将从 AttributeProviderAttribute 所返回的类型中而不是从实际属性 (Property) 类型获取属性 (Attribute)。此属性 (Attribute) 用在 DataGridView.DataSource 中,以使 DataGridView.DataSource 对象的特定类型指向 IListSource,并且相应的元数据放在 IListSource 中,以启用数据绑定。在这种情况下,可以从外部轻松地向所有数据源添加元数据。BindableAttribute view plaincopy to c
10、lipboardprint?01.Bindable(true) 02. public int MyProperty 03. get 04. / Insert code here. 05. return 0; 06. 07. set 08. / Insert code here. 09. 10. Bindable(true) public int MyProperty get / Insert code here. return 0; set / Insert code here. 大概含义:如果已将 BindableAttribute 设置为 true 来标记属性,则应引发该属性的属性更改通知
11、。这意味着,如果 Yes 属性 (Property) 为 Bindable,则支持双向数据绑定。如果 Bindable 是 No,则您仍可以绑定到该属性 (Property),但它不应该显示在默认的要绑定到的属性 (Property) 集中,因为它不一定引发属性 (Property) 更改通知。BrowsableAttribute view plaincopy to clipboardprint?01.Browsable(true) 02. public int MyProperty 03. get 04. / Insert code here. 05. return 0; 06. 07. s
12、et 08. / Insert code here. 09. 10. Browsable(true) public int MyProperty get / Insert code here. return 0; set / Insert code here. 大概含义:可视化设计器通常在“属性”窗口中显示没有可浏览属性 (Attribute) 的成员,或使用值 true 的 BrowsableAttribute 构造函数标记的成员。这些成员可以在设计时进行修改。使用值 false 的 BrowsableAttribute 构造函数标记的成员不适合在设计时进行编辑,因此,它们不会在可视化编辑器
13、中显示。默认为 true。CategoryAttribute view plaincopy to clipboardprint?01.Description(The image associated with the control),Category(Appearance) 02. public Image MyImage 03. get 04. / Insert code here. 05. return image1; 06. 07. set 08. / Insert code here. 09. 10. Description(The image associated with the
14、 control),Category(Appearance) public Image MyImage get / Insert code here. return image1; set / Insert code here. 一个 CategoryAttribute,它指示在一个设置为 Categorized 模式的 PropertyGrid 控件中列出属性 (Property) 或事件时,将关联的属性 (Property) 或事件与关联的类别。如果没有对属性或事件应用 CategoryAttribute,则 PropertyGrid 将属性 (Property) 或事件与“杂项”类别关联
15、。通过在 CategoryAttribute 的构造函数中指定类别的名称,可以为任何名称创建新的类别。Category 属性 (Property) 指示该属性 (Attribute) 所代表的类别的名称。Category 属性 (Property) 还以透明方式对类别名称进行本地化。给继承者的说明 如果使用类别名称而不是预定义的名称,并且想要本地化类别名称,则必须重写 GetLocalizedString 方法。此外,可以重写 Category 属性 (Property) 以提供您自己的本地化逻辑。 CategoryAttribute 类定义下列通用类别: 类别 说明 Action 与可用操作
16、相关的属性 (Property)。 Appearance 与实体的外观相关的属性 (Property)。 Behavior 与实体的行为相关的属性 (Property)。 Data 与数据和数据源管理相关的属性 (Property)。 Default 组合到默认类别中的属性 (Property)。 Design 仅在设计时可用的属性 (Property)。 DragDrop 与拖放操作相关的属性 (Property)。 Focus 与焦点相关的属性 (Property)。 Format 与格式设置相关的属性 (Property)。 Key 与键盘相关的属性 (Property)。 Layout
17、 与布局相关的属性 (Property)。 Mouse 与鼠标相关的属性 (Property)。 WindowStyle 与顶级窗体的窗口样式相关的属性 (Property)。 DataObjectAttributeview plaincopy to clipboardprint?01.DataObjectAttribute 02.public class NorthwindData 03. 04. public NorthwindData() 05. 06. DataObjectMethodAttribute(DataObjectMethodType.Select, true) 07. pu
18、blic static IEnumerable GetAllEmployees() 08. 09. AccessDataSource ads = new AccessDataSource(); 10. ads.DataSourceMode = SqlDataSourceMode.DataReader; 11. ads.DataFile = /App_Data/Northwind.mdb; 12. ads.SelectCommand = SELECT EmployeeID,FirstName,LastName FROM Employees; 13. return ads.Select(DataS
19、ourceSelectArguments.Empty); 14. 15. 16. / Delete the Employee by ID. 17. DataObjectMethodAttribute(DataObjectMethodType.Delete, true) 18. public void DeleteEmployeeByID(int employeeID) 19. 20. throw new Exception(The value passed to the delete method is 21. + employeeID.ToString(); 22. 23. DataObje
20、ctAttributepublic class NorthwindData public NorthwindData() DataObjectMethodAttribute(DataObjectMethodType.Select, true) public static IEnumerable GetAllEmployees() AccessDataSource ads = new AccessDataSource(); ads.DataSourceMode = SqlDataSourceMode.DataReader; ads.DataFile = /App_Data/Northwind.m
21、db; ads.SelectCommand = SELECT EmployeeID,FirstName,LastName FROM Employees; return ads.Select(DataSourceSelectArguments.Empty); / Delete the Employee by ID. DataObjectMethodAttribute(DataObjectMethodType.Delete, true) public void DeleteEmployeeByID(int employeeID) throw new Exception(The value pass
22、ed to the delete method is + employeeID.ToString(); 大概含义:使用 DataObjectAttribute 属性可以将某一对象标识为适合由 ObjectDataSource 对象使用。设计时类(如 ObjectDataSourceDesigner 类)使用 DataObjectAttribute 属性表示适合绑定到 ObjectDataSource 对象的对象。更多的见:利用属性扩展元数据。DataObjectFieldAttribute 类view plaincopy to clipboardprint?01.public class No
23、rthwindEmployee 02. 03. public NorthwindEmployee() 04. 05. private int _employeeID; 06. DataObjectFieldAttribute(true, true, false) 07. public int EmployeeID 08. 09. get return _employeeID; 10. set _employeeID = value; 11. 12. 13. private string _firstName = String.Empty; 14. DataObjectFieldAttribut
24、e(false, false, true) 15. public string FirstName 16. 17. get return _firstName; 18. set _firstName = value; 19. 20. 21. private string _lastName = String.Empty; 22. DataObjectFieldAttribute(false, false, true) 23. public string LastName 24. 25. get return _lastName; 26. set _lastName = value; 27. 2
25、8. public class NorthwindEmployee public NorthwindEmployee() private int _employeeID; DataObjectFieldAttribute(true, true, false) public int EmployeeID get return _employeeID; set _employeeID = value; private string _firstName = String.Empty; DataObjectFieldAttribute(false, false, true) public strin
26、g FirstName get return _firstName; set _firstName = value; private string _lastName = String.Empty; DataObjectFieldAttribute(false, false, true) public string LastName get return _lastName; set _lastName = value; 大概含义:使用 DataObjectFieldAttribute 属性可以提供有关基础数据架构的信息。设计时类(如 ObjectDataSourceDesigner 类)使用
27、 DataObjectAttribute 属性 (Attribute) 根据公开的架构在设计时设置属性 (Property)。将 DataObjectFieldAttribute 属性应用于数据项对象的成员,这些对象由通过 DataObjectAttribute 属性进行标记的对象的 Select 方法返回。在下面的示例中,NorthwindData 类使用 DataObjectAttribute 属性进行标记,并从 GetAllEmployees 方法返回一个包含 NorthwindEmployee 对象的 IEnumerable 对象。NorthwindEmployee 类中的字段使用 D
28、ataObjectFieldAttribute 属性进行标记,以指示它们表示基础数据源中的数据字段。有关使用属性的更多信息,请参见 利用属性扩展元数据。DataObjectMethodAttribute 类view plaincopy to clipboardprint?01.DataObjectAttribute 02.public class NorthwindData 03. 04. public NorthwindData() 05. 06. DataObjectMethodAttribute(DataObjectMethodType.Select, true) 07. public
29、static IEnumerable GetAllEmployees() 08. 09. AccessDataSource ads = new AccessDataSource(); 10. ads.DataSourceMode = SqlDataSourceMode.DataReader; 11. ads.DataFile = /App_Data/Northwind.mdb; 12. ads.SelectCommand = SELECT EmployeeID,FirstName,LastName FROM Employees; 13. return ads.Select(DataSource
30、SelectArguments.Empty); 14. 15. 16. / Delete the Employee by ID. 17. DataObjectMethodAttribute(DataObjectMethodType.Delete, true) 18. public void DeleteEmployeeByID(int employeeID) 19. 20. throw new Exception(The value passed to the delete method is 21. + employeeID.ToString(); 22. 23. DataObjectAtt
31、ributepublic class NorthwindData public NorthwindData() DataObjectMethodAttribute(DataObjectMethodType.Select, true) public static IEnumerable GetAllEmployees() AccessDataSource ads = new AccessDataSource(); ads.DataSourceMode = SqlDataSourceMode.DataReader; ads.DataFile = /App_Data/Northwind.mdb; a
32、ds.SelectCommand = SELECT EmployeeID,FirstName,LastName FROM Employees; return ads.Select(DataSourceSelectArguments.Empty); / Delete the Employee by ID. DataObjectMethodAttribute(DataObjectMethodType.Delete, true) public void DeleteEmployeeByID(int employeeID) throw new Exception(The value passed to
33、 the delete method is + employeeID.ToString(); 一般含义:可以使用 DataObjectMethodAttribute 标识以 DataObjectAttribute 属性进行标记的型上的数据操作方法,以便调用方可以通过使用反射更容易地标识这些方法。在将 DataObjectMethodAttribute 属性应用于某一方法时,该属性描述该方法所执行的操作类型并指示该方法是否是某一类型的默认数据操作方法。组件(如 ObjectDataSource 控件和 ObjectDataSourceDesigner 类)检查此属性值(如果提供的话)以帮助确定在
34、运行时调用哪一数据方法。DefaultBindingPropertyAttribute 类view plaincopy to clipboardprint?01./ This control demonstrates a simple logging capability. 02.ComplexBindingProperties(DataSource, DataMember) 03.DefaultBindingProperty(TitleText) 04.DefaultEvent(ThresholdExceeded) 05.DefaultProperty(Threshold) 06.HelpK
35、eywordAttribute(typeof(UserControl) 07.ToolboxItem(System.Windows.Forms.Design.AutoSizeToolboxItem,System.Design) 08.public class AttributesDemoControl : UserControl 09. / This control demonstrates a simple logging capability. ComplexBindingProperties(DataSource, DataMember)DefaultBindingProperty(Ti
36、tleText)DefaultEvent(ThresholdExceeded)DefaultProperty(Threshold)HelpKeywordAttribute(typeof(UserControl)ToolboxItem(System.Windows.Forms.Design.AutoSizeToolboxItem,System.Design)public class AttributesDemoControl : UserControl DefaultBindingPropertyAttribute 是在类级别指定的。它可以被继承,但不允许在同一类中存在多个属性。 有关使用属性的
37、更多信息,请参见 利用属性扩展元数据。 DefaultEventAttribute 类view plaincopy to clipboardprint?01.DefaultEvent(CollectionChanged) 02.public class MyCollection : BaseCollection 03. 04. private CollectionChangeEventHandler onCollectionChanged; 05. 06. public event CollectionChangeEventHandler CollectionChanged 07. add 0
38、8. onCollectionChanged += value; 09. 10. remove 11. onCollectionChanged -= value; 12. 13. 14. / Insert additional code. 15. DefaultEvent(CollectionChanged)public class MyCollection : BaseCollection private CollectionChangeEventHandler onCollectionChanged; public event CollectionChangeEventHandler Co
39、llectionChanged add onCollectionChanged += value; remove onCollectionChanged -= value; / Insert additional code. 大概含义:属性来获取默认事件的名称。DefaultPropertyAttribute 类 view plaincopy to clipboardprint?01.DefaultProperty(MyProperty) 02. public class MyControl : Control 03. 04. public int MyProperty 05. get 06.
40、 / Insert code here. 07. return 0; 08. 09. set 10. / Insert code here. 11. 12. 13. 14. / Insert any additional code. 15. 16. DefaultProperty(MyProperty) public class MyControl : Control public int MyProperty get / Insert code here. return 0; set / Insert code here. / Insert any additional code. 大概含义
41、:使用 Name 属性来获取默认属性的名称。DefaultValueAttribute 类view plaincopy to clipboardprint?01.private bool myVal=false; 02. 03.DefaultValue(false) 04. public bool MyProperty 05. get 06. return myVal; 07. 08. set 09. myVal=value; 10. 11. private bool myVal=false;DefaultValue(false) public bool MyProperty get retu
42、rn myVal; set myVal=value; 大概含义:可以使用任何值创建 DefaultValueAttribute。成员的默认值通常是其初始值。可视化设计器可以使用默认值重置成员的值。代码生成器也可使用默认值确定是否为成员生成代码。有关更多信息,请参见 属性 (Attribute) 概述 和 利用属性扩展元数据。DescriptionAttribute 类view plaincopy to clipboardprint?01.Description(The image associated with the control),Category(Appearance) 02. pub
43、lic Image MyImage 03. get 04. / Insert code here. 05. return image1; 06. 07. set 08. / Insert code here. 09. 10. Description(The image associated with the control),Category(Appearance) public Image MyImage get / Insert code here. return image1; set / Insert code here. 大概含义:可视化设计器在引用组件成员时可以显示指定的说明,如在
44、“属性”窗口中。调 Description 访问该属性 (Attribute) 的值。有关更多信息,请参见 属性 (Attribute) 概述 和 利用属性扩展元数据。DesignerAttribute 类view plaincopy to clipboardprint?01.Designer(System.Windows.Forms.Design.DocumentDesigner, System.Windows.Forms.Design.DLL, 02. typeof(IRootDesigner), 03. DesignerCategory(Form) 04.public class MyF
45、orm : ContainerControl 05. / Insert code here. 06. Designer(System.Windows.Forms.Design.DocumentDesigner, System.Windows.Forms.Design.DLL, typeof(IRootDesigner), DesignerCategory(Form)public class MyForm : ContainerControl / Insert code here. 大概含义:用于设计时服务的类必须实现 IDesigner 接口。使用 DesignerBaseTypeName 属
46、性查找设计器的基类。使用 DesignerTypeName 属性获取与该成员关联的设计器的类型名称。DesignerCategoryAttribute 类view plaincopy to clipboardprint?01.Designer(System.Windows.Forms.Design.DocumentDesigner, System.Windows.Forms.Design, 02. typeof(IRootDesigner), 03. DesignerCategory(Form) 04. 05. public class MyForm : ContainerControl 06
47、. / Insert code here. 07. Designer(System.Windows.Forms.Design.DocumentDesigner, System.Windows.Forms.Design, typeof(IRootDesigner), DesignerCategory(Form) public class MyForm : ContainerControl / Insert code here. 大概用法:可视化设计器可以使用设计器类别通知开发环境将要实现的设计器类型。如果没有为某个类提供任何设计器类别,开发环境可能允许设计此类,也可能不允许。可以创建任何名称的类别。当用此属性标记类时,它被设置为常数成员。当要在代码中检查此属性的值时,必须指定常数成员。下表中的“说明”列列出了将每个值设置为的常数成员。DesignOnlyAttribute 类view plaincopy to clipboardprint?01.DesignOnly(true) 02. public CultureInfo GetLanguage 03. get 04. / Insert code here. 05. return myCultureInfo; 06. 07. set 08. / Insert code here. 09. 10. Des
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 徐工集团笔试真题解析
- 聆听大自然作文
- 湖南小升初生物衔接试题及答案
- 教师在线易错题及答案解析
- 初中语法知识相关默写题目及答案
- 人才保险练习题及正确答案
- 幼小衔接家长课:入学焦虑疏导
- 2026年财务岗位考试模拟题及答案详解
- 2027届吉林省长春市德惠三中学数学七上期末综合测试模拟试题含解析
- 司法助理模拟试题及答案参考
- IC芯片焊接课件
- 2025至2030中国自适应光学元件行业市场深度研究与战略咨询分析报告
- 金钥匙科技竞赛题库及答案
- 地磅培训知识课件
- 急性盆腔炎护理查房课件
- (正式版)DB42∕T 2305-2024 《高品质住宅技术标准》
- DB14∕T 3151-2024 公路钢波纹管涵洞施工技术规程
- 《关于严格规范涉企行政检查》知识培训
- 人工智能导论知到智慧树章节测试课后答案2024年秋天津大学
- 第六章 人体生命活动的调节【单元测试·提升卷】(原卷版)
- NB-T 20580.9-2021 核电厂建设工程概算定额 第9部分:常规岛电气设备安装工程
评论
0/150
提交评论