Custom Editor via CustomTypeDescriptor

Grids for WPF Forum

Posted 8 years ago by Tony Juricic
Version: 11.2.0551
Avatar
I have a nested property (that is, property on an object which itself is a property of another object) of type string and name "Name". Because it is so generic I can not use property type or property name to configure a custom editor for it. Furthermore, I have no access to the original object source code so I cannot annotate the property with Editor attribute.

Thus I am using CustomTypeDescriptor to wrap properties in a custom descriptor:

        public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            var props = new PropertyDescriptorCollection(null);
            foreach (PropertyInfo pi in _objectType.GetProperties())
            {
                props.Add(new SimplePropertyDescriptor(pi, attributes));
            }
            // Return the computed properties
            return props;
       }
SimplePropertyDescriptor can now return custom editor for a given property:

        public override object GetEditor(Type editorBaseType)
        {
            if (_prop.DeclaringType == typeof(MyInteropKey))
            {
                if (_prop.Name == "Name")
                {
                    return new CustomNamePropertyEditor();
                }
            }
            return base.GetEditor(editorBaseType);
        }
I used /ProductSamples/EditorsSamples/Demo/PropertyGridIntegrationCustom sample
to create my CustomNamePropertyEditor class, based on RectPropertyEditor.

All is fine and all works as expected. My editor gets shown and used for a nested property.

However, while in debugger I noticed that GetEditor() method gets called at least 5 times for a single property instance. Since I return new CustomNamePropertyEditor object each time, I am now concerned about having abandoned objects, possibly carrying some state info, hanging around. Furthermore, said property is used multiple times in the collection of objects having that property, so we are talking about possible hundreds of calls to GetEditor(), each returning a fresh editor object.

Can you shed some light on how does PropertyGrid use the value returned by GetEditor(), in consideration of it possibly being a fresh object returned after each call?

Comments (1)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Tony,

The GetEditor method can be called once for each of the PropertyEditor properties, which include NameTemplate, NameTemplateKey, NameTemplateSelector, ValueTemplate, ValueTemplateKey, and ValueTemplateSelector.

Normally, a single instance of the resulting editor is always returned. So you could update your code like so:
private static CustomNamePropertyEditor nameEditor = new CustomNamePropertyEditor();
//...
public override object GetEditor(Type editorBaseType)
{
    if (_prop.DeclaringType == typeof(MyInteropKey))
    {
        if (_prop.Name == "Name")
        {
            return nameEditor;
        }
    }
    return base.GetEditor(editorBaseType);
}


Actipro Software Support

The latest build of this product (v2019.1 build 0682) was released 4 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.