PropertyGrid use summary 4 IcustomTypeDescriptor

Previous section illustrates an important class TypeConverter, some objects need to provide a description of the custom of the time, TypeConverter may not be satisfied, in those cases, you need to implement a custom description of it, such as the following requirements:

  1. When the object needs dynamic type information needed when self-describing.
  2. COM type information objects, COM object does not support the property or properties required IcustomTypeDescriptor class encapsulates.

In this chapter we According to these two requirements, introduced the current application interface.

In order to achieve the objects can use the same components when I let the class inherits the Component class code is as follows:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace AlbertControlExample.Controls

{

/// <summary>

/// definition of a component

/// </summary>

public class CustomDef : Component, ICustomTypeDescriptor

{

public CustomDef()

{

Left = 0;

Top = 0;

}

public CustomDef(int left, int top)

{

this.Left = left;

this.Top = top;

}

public double Left { get; set; }

public double Top { get; set; }

 

/// <summary>

// Get the current set of attributes

///</summary>

/// <returns></returns>

public AttributeCollection GetAttributes()

{

return TypeDescriptor.GetAttributes(this, true);

}

 

public string GetClassName()

{

return "class name" ;

}

 

public string GetComponentName()

{

return "component name" ;

}

 

/// <summary>

/// current object TypeConverter

/// </summary>

/// <returns></returns>

public TypeConverter GetConverter()

{

return TypeDescriptor.GetConverter(this, true);

}

///<summary>

/// Return the current event descriptor

///</summary>

///<returns></returns>

public EventDescriptor GetDefaultEvent()

{

return TypeDescriptor.GetDefaultEvent(this, true);

}

 

/// <summary>

/// 返回当前的默认属性

/// </summary>

/// <returns></returns>

public PropertyDescriptor GetDefaultProperty()

{

return TypeDescriptor.GetDefaultProperty(this, true);

}

 

/// <summary>

/// 返回当前的编辑器

/// </summary>

/// <param name="editorBaseType"></param>

/// <returns></returns>

public object GetEditor(Type editorBaseType)

{

return TypeDescriptor.GetEditor(this, editorBaseType, true);

}

 

/// <summary>

/// 返回当前的事件集合

/// </summary>

/// <returns></returns>

public EventDescriptorCollection GetEvents()

{

return TypeDescriptor.GetEvents(this, true);

}

 

/// <summary>

/// 返回当前的事件描述集合

/// </summary>

/// <param name="attributes"></param>

/// <returns></returns>

public EventDescriptorCollection GetEvents(Attribute[] attributes)

{

return TypeDescriptor.GetEvents(this, attributes, true);

}

 

/// <summary>

/// 返回当前的属性集合

/// </summary>

/// <returns></returns>

public PropertyDescriptorCollection GetProperties()

{

PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this, true);

 

return properties;

}

 

/// <summary>

/// 返回当前的属性描述集合

/// </summary>

/// <param name="attributes"></param>

/// <returns></returns>

public PropertyDescriptorCollection GetProperties(Attribute[] attributes)

{

PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this, attributes, true);

 

return properties;

}

 

/// <summary>

/// 返回当前属性的宿主

/// </summary>

/// <param name="pd"></param>

/// <returns></returns>

public object GetPropertyOwner(PropertyDescriptor pd)

{

return this;

}

}

}

我们分别说明每个函数的作用和意义:

public string GetClassName()

{

return "类名称";

}

 

public string GetComponentName()

{

return "组件名称";

}

这两个主要用于显示的目的,如图

/// <summary>

/// 返回当前的默认事件

/// </summary>

/// <returns></returns>

public EventDescriptor GetDefaultEvent()

{

return TypeDescriptor.GetDefaultEvent(this, true);

}

 

/// <summary>

/// 返回当前的默认属性

/// </summary>

/// <returns></returns>

public PropertyDescriptor GetDefaultProperty()

{

PropertyDescriptor propertyDescriptor = TypeDescriptor.CreateProperty(typeof(CustomDef),"Left",typeof(double));

return propertyDescriptor;

}

这两个函数很特殊,其实就是显示默认的属性和双击控件默认的生成事件,如下图,优先显示的属性

/// <summary>

/// 返回当前的事件集合

/// </summary>

/// <returns></returns>

public EventDescriptorCollection GetEvents()

{

return TypeDescriptor.GetEvents(this, true);

}

 

/// <summary>

/// 返回当前的事件描述集合

/// </summary>

/// <param name="attributes"></param>

/// <returns></returns>

public EventDescriptorCollection GetEvents(Attribute[] attributes)

{

return TypeDescriptor.GetEvents(this, attributes, true);

}

 

/// <summary>

/// 返回当前的属性集合

/// </summary>

/// <returns></returns>

public PropertyDescriptorCollection GetProperties()

{

PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this, true);

 

return properties;

}

 

/// <summary>

/// 返回当前的属性描述集合

/// </summary>

/// <param name="attributes"></param>

/// <returns></returns>

public PropertyDescriptorCollection GetProperties(Attribute[] attributes)

{

PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this, attributes, true);

 

return properties;

}

 

用于获取当前的对象的属性列表和事件列表,也就是我们所有显示的列表对象

/// <summary>

/// 返回当前的编辑器

/// </summary>

/// <param name="editorBaseType"></param>

/// <returns></returns>

public object GetEditor(Type editorBaseType)

{

return TypeDescriptor.GetEditor(this, editorBaseType, true);

}

用于获取当前的类型编辑器,这个在下一章会详细介绍。

其他函数,比较简单,很容易理解。

Guess you like

Origin www.cnblogs.com/minhost/p/12300751.html