Pregunta

Estoy personalizar cómo se muestra un tipo de objeto en un PropertyGrid mediante la implementación de ICustomTypeDescriptor. Estoy permitiendo al usuario crear sus propias propiedades personalizadas que se almacenan en un único diccionario de claves y valores. Soy capaz de crear todo el 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 habría mostrado si el PropertyGrid fue poblada por la reflexión en lugar de mi método de reemplazo ICustomTypeDescriptor.GetProperties.

Ahora sé cómo obtener el tipo de objeto y, a continuación, 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 personalizada?

//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);
¿Fue útil?

Solución

PropertyDescriptorCollection props = TypeDescriptor.GetProperties(thisType);

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

(edit) Como un 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);
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top