views:

598

answers:

2

Hello,

I'm writing a GUI application where I need to enable editing properties of arbitrary objects (their types are only known at run-time).

I've decided to use the PropertyGrid control to enable this functionality. I created the following class:

[TypeConverter(typeof(ExpandableObjectConverter))]
[DefaultPropertyAttribute("Value")]
public class Wrapper
{
        public Wrapper(object val)
        {
            m_Value = val;
        }

        private object m_Value;

        [NotifyParentPropertyAttribute(true)]
        [TypeConverter(typeof(ExpandableObjectConverter))]
        public object Value
        {
            get { return m_Value; }
            set { m_Value = value; }
        }
}

When I get an instance of an object I need to edit, I create a Wrapper for it and set it as the selected object:

Wrapper wrap = new Wrapper(obj);
propertyGrid.SelectedObject = wrap;

But I've run into the following problem - the above works as expected only when the type of obj is some custom type (i.e a class that I defined by myself, or a built in complex type) but not when obj is a primitive.

For example, if I define:

[TypeConverter(typeof(ExpandableObjectConverter))]
public class SomeClass
{
        public SomeClass()
        {
            a = 1;
            b = 2;
        }

        public SomeClass(int a, int b)
        {
            this.a = a;
            this.b = b;
        }

        private int a;

        [NotifyParentPropertyAttribute(true)]
        public int A
        {
            get { return a; }
            set { a = value; }
        }

        private int b;

        [NotifyParentPropertyAttribute(true)]
        public int B
        {
            get { return b; }
            set { b = value; }
        }
}

And do:

Wrapper wrap = new Wrapper(new SomeClass());
propertyGrid.SelectedObject = wrap;

Then everything works swell. On the other hand, when I perform the following:

int num = 1;
Wrapper wrap = new Wrapper(num);
propertyGrid.SelectedObject = wrap;

Then I can see the value "1" in the grid (and it's not grayscaled) but I can't edit the value. I noticed that if I change Wrapper's "Value" property's type to int and remove the TypeConverter attribute, it works. I get the same behavior for other primitive types and strings.

What is the problem?

Thanks in advance!

A: 

Remove the "TypeConverter" from property "Value", the propertygrid will read the the "TypConverter" from typeo of value which is in property.

TcKs
I tried it, when I do this the property grid displays the value of the property correctly, but gray-scaled and editing is disabled (it does this even when obj is not a primitive)
Marina
Unfortunately, the MS PropertyGrid is not that smart hwne you set no converter to your Value property. For your int, it won't return Int32Converter but TypeConverter. I know it because I had to handle this case in SPG. SEe my answer for a workaround.
Nicolas Cadilhac
Hmm, you are right. Until now I lived in conviction, that property grid is so smart. Thanks
TcKs
+2  A: 

If you set ExpandableObjectConverter to your Value property, it won't be editable and this is normal because CanConvertFrom will return false. IF you remove the type converter, the PropertyGrid will use the generic TypeConverter and you are again in the same case. So the workaround is to attach a smarter TypeConverter that will act as a wrapper to the correct TypeConverter. Here is a dirty one (I had not much time, you will complete it as needed since I just implemented the ConvertFrom part):

public class MySmartExpandableObjectConverter : ExpandableObjectConverter
{
    TypeConverter actualConverter = null;

    private void InitConverter(ITypeDescriptorContext context)
    {
        if (actualConverter == null)
        {
            TypeConverter parentConverter = TypeDescriptor.GetConverter(context.Instance);
            PropertyDescriptorCollection coll = parentConverter.GetProperties(context.Instance);
            PropertyDescriptor pd = coll[context.PropertyDescriptor.Name];

            if (pd.PropertyType == typeof(object))
                actualConverter = TypeDescriptor.GetConverter(pd.GetValue(context.Instance));
            else
                actualConverter = this;
        }
    }

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        InitConverter(context);

        return actualConverter.CanConvertFrom(context, sourceType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        InitConverter(context); // I guess it is not needed here

        return actualConverter.ConvertFrom(context, culture, value);
    }
}

Let me know if you need to finetune something.

Nicolas

Nicolas Cadilhac
Thanks a lot, that did the trick! :)
Marina