locked
PropertyGrid and dynamic types of objects RRS feed

  • Question

  • Hello,

    I'm facing the following problem using PropertyGrid.

    I have a class that has a private field of type object, that is initialized to an instance of some class that is only known at runtime.
    I have a matching property defined for this field of the form:

    public object MyField
    {
         get
         {
               return myField;
         }
         set
         {
               myField = value;
         }
    }

    The problem is that PropertyGrid ignores the dynamic type of the object contained in myField, and treats it as if it was an instance of type object, thus not expanding the type or allows editing.
    Is there a way to make PropertyGrid refer to the dynamic type of the field?

    Thanks in advance!


     
    Friday, December 4, 2009 12:54 AM

Answers

  • Hi,

    use this code instead:
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public object MyField
    { .......


    -----------------------------------------------------------------------
    Best regards.

    Flexible Treeview - the most flexible treeview-listview-grid hybrid control for .NET!
    • Proposed as answer by Ruslan Mogilevskiy Friday, December 4, 2009 8:11 PM
    • Marked as answer by Jing0 Wednesday, December 9, 2009 8:23 AM
    Friday, December 4, 2009 8:45 AM

All replies

  • Hi,

    use this code instead:
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public object MyField
    { .......


    -----------------------------------------------------------------------
    Best regards.

    Flexible Treeview - the most flexible treeview-listview-grid hybrid control for .NET!
    • Proposed as answer by Ruslan Mogilevskiy Friday, December 4, 2009 8:11 PM
    • Marked as answer by Jing0 Wednesday, December 9, 2009 8:23 AM
    Friday, December 4, 2009 8:45 AM
  • Thanks a lot, that solved the problem!

    I thought that attribute was supposed to go above the class declaration.
    Friday, December 4, 2009 11:05 AM
  • I've run into another problem;
    When the type of the object I assign to myField is a custom type (i.e a class that I defined by myself, or some built in complex type), it is displayed as expected in the PropertyGrid and I can expand complex properties and edit the values.
    On the other hand, when the type of the object is some value type or a string, the PropertyGrid displays it properly (not in gray scale), but I can't edit the values.

    For example, given that my class is defined as follows:

    [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; }
            }
    }


    and 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; }
            }
    }


    Then if I perform the following:

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


    Then everything works swell.
    If, on the other hand I perform the following code:

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


    Then I can see the value "1" in the grid but can't edit it.

    What is the problem?

    Thanks in advance!
    Tuesday, December 8, 2009 6:56 PM