PropertyGrid使用总结4 IcustomTypeDescriptor
前面章节说明了一个重要的类TypeConverter,有些对象需要提供自定义的描述的时候,TypeConverter可能就不满足,在那些情况下,需要实现自定义的描述呢, 比如以下需求:
- 当对象需要动态类型信息时,需要自描述的时候。
- COM 对象的类型信息,COM 对象不支持属性或属性,需要使用IcustomTypeDescriptor类封装。
本章我们根据这两个需求,分别介绍当前接口的应用。
为了实现对象可以当组件一样使用,我让类继承了Component类,代码如下:
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>
/// 定义一个组件
/// </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>
/// 获取当前attributes集合
/// </summary>
/// <returns></returns>
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}
public string GetClassName()
{
return "类名称";
}
public string GetComponentName()
{
return "组件名称";
}
/// <summary>
/// 当前对象的TypeConverter
/// </summary>
/// <returns></returns>
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}
/// <summary>
/// 返回当前的事件描述器
/// </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;
}
}
}
我们分别说明每个函数的作用和意义。
1、显示一些名称
public string GetClassName() { return "类名称"; } public string GetComponentName() { return "组件名称"; }
这两个主要用于显示的目的,如图

2、默认属性的默认事件
/// <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; }
这两个函数很特殊,其实就是显示默认的属性和双击控件默认的生成事件,如下图,优先显示的属性

3、当前的属性集合和事件集合
/// <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; }
4、编辑器对象
用于获取当前的对象的属性列表和事件列表,也就是我们所有显示的列表对象
/// <summary> /// 返回当前的编辑器 /// </summary> /// <param name="editorBaseType"></param> /// <returns></returns> public object GetEditor(Type editorBaseType) { return TypeDescriptor.GetEditor(this, editorBaseType, true); }
用于获取当前的类型编辑器,这个在下一章会详细介绍。
其他函数,比较简单,很容易理解。

浙公网安备 33010602011771号