Grid fails to expand objects with custom ExpandableObjectConverter attribute

Mar 25, 2011 at 6:09 PM

I found an issue with the grid when an object that has a custom ExpandableObjectConverter attribute attached to the class:

 [TypeConverter(typeof(ParameterExpandableObjectConverter))]
    public class ParameterObject_BandBch : DependencyObject, INotifyPropertyChanged...
    

The grid fails to recognize that the object has sub-properties.

In module 'PropertyCollection.cs' I made the following changes to get the grid to display the sub-properties:

        private void CollectProperties(object instance, PropertyDescriptor descriptor, List propertyCollection, bool automaticlyExpandObjects, string filter)
        {

            if (descriptor.Attributes[typeof(FlatAttribute)] == null)
            {
                Property property = new Property(instance, descriptor);
                if (descriptor.IsBrowsable)
                {
                    //Add a property with Name: AutomaticlyExpandObjects
                    Type propertyType = descriptor.PropertyType;
                    if (automaticlyExpandObjects && propertyType.IsClass && !propertyType.IsArray && propertyType != typeof(string))
                    {
                        propertyCollection.Add(new ExpandableProperty(instance, descriptor, automaticlyExpandObjects, filter));
                    }
                    else if (descriptor.Converter is ExpandableObjectConverter) // Changed to 'is' operator instead of 'descriptor.Converter.GetType() == typeof(ExpandableObjectConverter)' so derived classes will also get expanded. P.D.
                    {
                        propertyCollection.Add(new ExpandableProperty(instance, descriptor, automaticlyExpandObjects, filter));
                    }
                    else
                        propertyCollection.Add(property);
                }
            }
            else
            {
                instance = descriptor.GetValue(instance);
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(instance);
                foreach (PropertyDescriptor propertyDescriptor in properties)
                {
                    CollectProperties(instance, propertyDescriptor, propertyCollection, automaticlyExpandObjects, filter);
                }
            }
        }
	And changed the constructor to:
        public PropertyCollection(object instance, bool noCategory, bool automaticlyExpandObjects, string filter)
        {
            Dictionary groups = new Dictionary();

            bool useCustomTypeConverter = false;

            PropertyDescriptorCollection properties;
            if (instance != null)
            {
                TypeConverter tc = TypeDescriptor.GetConverter(instance);
                if (tc == null || !tc.GetPropertiesSupported())
                {

                    if (instance is ICustomTypeDescriptor)
                        properties = ((ICustomTypeDescriptor)instance).GetProperties();
                    else
                        properties = TypeDescriptor.GetProperties(instance.GetType());  //I changed here from instance to instance.GetType, so that only the Direct Properties are shown!
                }
                else
                {
                    properties = tc.GetProperties(instance);
                    // Added this line of code to force expandable objects to load there sub-parameters, P.D.
                    if (!(tc is ExpandableObjectConverter)) useCustomTypeConverter = true;
                }

Also, when there are multiple levels of sub-items, the drag bar between the parameter names and parameter values gets offset. Hit-testing is wrong when there are multiple levels of sub-items in the grid.