2009-04-09 16 views
9

Estoy personalizando cómo se muestra un tipo de objeto en un PropertyGrid implementando ICustomTypeDescriptor. Permitiré al usuario crear sus propias propiedades personalizadas que están almacenadas en un solo diccionario de claves y valores. Puedo crear todos los PropertyDescriptors para estos valores y verlos en la cuadrícula de propiedades. Sin embargo, también quiero mostrar todas las propiedades predeterminadas que de otro modo se mostrarían si el PropertyGrid se llenó mediante reflexión en lugar de reemplazar el método ICustomTypeDescriptor.GetProperties.Obtenga los PropertyDescriptors predeterminados para un tipo

Ahora sé cómo obtener el tipo de objeto, y luego GetProperties(), pero esto devuelve una matriz de PropertyInfo no ProperyDescriptor. Entonces, ¿cómo puedo convertir el objeto PropertyInfo del tipo en objetos PropertyDescriptor para incluir en mi colección con el PropertyDescriptors personalizado?

//gets the local intrinsic properties of the object 
Type thisType = this.GetType(); 
PropertyInfo[] thisProps = thisType.GetProperties(); 

//this line obviously doesn't work because the propertydescriptor 
//collection needs an array of PropertyDescriptors not PropertyInfo 
PropertyDescriptorCollection propCOl = 
    new PropertyDescriptorCollection(thisProps); 

Respuesta

15
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(thisType); 

Como acotación al margen: esto no va a incluir sus ICustomTypeDescriptor personalizaciones, pero se incluir cualquier personalizaciones realizadas a través de TypeDescriptionProvider.

(editar) Como segundo lado - también se puede ajustar PropertyGrid proporcionando un TypeConverter - mucho más simple que sea ICustomTypeDescriptor o TypeDescriptionProvider - por ejemplo:

[TypeConverter(typeof(FooConverter))] 
class Foo { } 

class FooConverter : ExpandableObjectConverter 
{ 
    public override PropertyDescriptorCollection GetProperties(
     ITypeDescriptorContext context, object value, Attribute[] attributes) 
    { 
     // your code here, perhaps using base.GetPoperties(
     // context, value, attributes); 
    } 
} 
+0

Gracias tanto! – Ross

Cuestiones relacionadas