CategoryAttribute 类

指定当属性 (Property) 或事件显示在一个设置为“按分类顺序”模式的 PropertyGrid 控件中时,用于给属性或事件分组的类别的名称。

**命名空间:**System.ComponentModel
**程序集:**System(在 system.dll 中)

语法

声明
<AttributeUsageAttribute(AttributeTargets.All)> _
Public Class CategoryAttribute
    Inherits Attribute
用法
Dim instance As CategoryAttribute
[AttributeUsageAttribute(AttributeTargets.All)] 
public class CategoryAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::All)] 
public ref class CategoryAttribute : public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.All) */ 
public class CategoryAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.All) 
public class CategoryAttribute extends Attribute

备注

一个 CategoryAttribute,它指示在一个设置为 Categorized 模式的 PropertyGrid 控件中列出属性 (Property) 或事件时,将关联的属性 (Property) 或事件与之关联的类别。如果没有对属性或事件应用 CategoryAttribute,则 PropertyGrid 将属性 (Property) 或事件与“杂项”类别关联。通过在 CategoryAttribute 的构造函数中指定类别的名称,可以为任何名称创建新的类别。

Category 属性 (Property) 指示该属性 (Attribute) 所代表的类别的名称。Category 属性 (Property) 还以透明方式对类别名称进行本地化。

给继承者的说明 如果使用类别名称而不是预定义的名称,并且想要本地化类别名称,则必须重写 GetLocalizedString 方法。此外,可以重写 Category 属性 (Property) 以提供您自己的本地化逻辑。 CategoryAttribute 类定义下列通用类别:

类别

说明

Action

与可用操作相关的属性 (Property)。

Appearance

与实体的外观相关的属性 (Property)。

Behavior

与实体的行为相关的属性 (Property)。

Data

与数据和数据源管理相关的属性 (Property)。

Default

组合到默认类别中的属性 (Property)。

Design

仅在设计时可用的属性 (Property)。

DragDrop

与拖放操作相关的属性 (Property)。

Focus

与焦点相关的属性 (Property)。

Format

与格式设置相关的属性 (Property)。

Key

与键盘相关的属性 (Property)。

Layout

与布局相关的属性 (Property)。

Mouse

与鼠标相关的属性 (Property)。

WindowStyle

与顶级窗体的窗口样式相关的属性 (Property)。

有关更多信息,请参见 属性 (Attribute) 概述利用属性扩展元数据

主题 位置
演练:开发和使用自定义服务器控件 创作 ASP.NET 控件
为 ASP.NET 1.1 开发自定义数据绑定 Web 服务器控件 创作 ASP.NET 控件
为 ASP.NET 2.0 开发自定义数据绑定 Web 服务器控件 创作 ASP.NET 控件
演练:为 ASP.NET 2.0 创建自定义数据绑定 ASP.NET Web 控件 创作 ASP.NET 控件
演练:为 ASP.NET 1.1 创建自定义数据绑定 ASP.NET Web 控件 创作 ASP.NET 控件
演练:开发和使用自定义服务器控件 使用 Visual Web Developer 生成应用程序
为 ASP.NET 2.0 开发自定义数据绑定 Web 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:为 ASP.NET 2.0 创建自定义数据绑定 ASP.NET Web 控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
为 ASP.NET 1.1 开发自定义数据绑定 Web 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
为 ASP.NET 2.0 开发自定义数据绑定 Web 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:为 ASP.NET 2.0 创建自定义数据绑定 ASP.NET Web 控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:为 ASP.NET 1.1 创建自定义数据绑定 ASP.NET Web 控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
为 ASP.NET 1.1 开发自定义数据绑定 Web 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:为 ASP.NET 1.1 创建自定义数据绑定 ASP.NET Web 控件 在 Visual Studio 中生成 ASP .NET Web 应用程序

示例

下面的示例创建 MyImage 属性 (Property)。该属性 (Property) 有 DescriptionAttributeCategoryAttribute 两个属性 (Attribute)。

<Description("The image associated with the control"), _
    Category("Appearance")> _
Public Property MyImage() As Image
    
    Get
        ' Insert code here.
        Return image1
    End Get
    Set
        ' Insert code here.
    End Set 
End Property
[Description("The image associated with the control"),Category("Appearance")] 
 public Image MyImage {
    get {
       // Insert code here.
       return image1;
    }
    set {
       // Insert code here.
    }
 }
   [Description("The image associated with the control"),Category("Appearance")]
   System::Drawing::Image^ get()
   {
      // Insert code here.
      return m_Image1;
   }

   void set( System::Drawing::Image^ )
   {
      // Insert code here.
   }
}
/** @attribute Description("The image associated with the control")
  * @attribute Category("Appearance")
 */
/** @property 
 */
public Image get_MyImage()
{
    // Insert code here.
    return image1;
} //get_MyImage

/** @property 
 */
public void set_MyImage(Image value)
{
    // Insert code here.
} //set_MyImage

下一个示例获取 MyImage 的类别。首先,代码获取具有该对象的所有属性 (Property) 的 PropertyDescriptorCollection。接下来,代码对 PropertyDescriptorCollection 进行索引,以获取 MyImage。然后返回该属性 (Property) 的属性 (Attribute),并将它们保存到 attributes 变量中。

然后,该示例通过以下方法输出类别:从 AttributeCollection 检索 CategoryAttribute,然后将它写到控制台屏幕上。

' Gets the attributes for the property.
Dim attributes As AttributeCollection = _
    TypeDescriptor.GetProperties(Me)("MyImage").Attributes

' Prints the description by retrieving the CategoryAttribute. 
' from the AttributeCollection.
Dim myAttribute As CategoryAttribute = _
    CType(attributes(GetType(CategoryAttribute)), CategoryAttribute)
    Console.WriteLine(myAttribute.Category)
// Gets the attributes for the property.
 AttributeCollection attributes = 
    TypeDescriptor.GetProperties(this)["MyImage"].Attributes;
 
 // Prints the description by retrieving the CategoryAttribute.
 // from the AttributeCollection.
 CategoryAttribute myAttribute = 
    (CategoryAttribute)attributes[typeof(CategoryAttribute)];
 Console.WriteLine(myAttribute.Category);
// Gets the attributes for the property.
AttributeCollection^ attributes = TypeDescriptor::GetProperties( this )[ "MyImage" ]->Attributes;

// Prints the description by retrieving the CategoryAttribute.
// from the AttributeCollection.
CategoryAttribute^ myAttribute = static_cast<CategoryAttribute^>(attributes[ CategoryAttribute::typeid ]);
Console::WriteLine( myAttribute->Category );
// Gets the attributes for the property.
AttributeCollection attributes = TypeDescriptor.GetProperties(this)
    .get_Item("MyImage").get_Attributes();

// Prints the description by retrieving the CategoryAttribute.
// from the AttributeCollection.
CategoryAttribute myAttribute = (CategoryAttribute)(
    attributes.get_Item(CategoryAttribute.class.ToType()));
Console.WriteLine(myAttribute.get_Category());

继承层次结构

System.Object
   System.Attribute
    System.ComponentModel.CategoryAttribute

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

CategoryAttribute 成员
System.ComponentModel 命名空间
Attribute
PropertyDescriptor
EventDescriptor