none
How to expand nested Complex types in propertygrid? RRS feed

  • Question

  • Hi
        I'm using propertygrid in my code to display class properties in it. My Cod is just like:
    public partial class AddressType
    {
    .......
    .......
    }

    public partial class Person {

    private AddressType homeAddressField;

    public AddressType HomeAddress
    {
               
        get {
                    return this.homeAddressField;
                }
           
        set {
                    this.homeAddressField = value;
                }
    }
           
    }


    PropertyGrid just shows the Person Having field HomeAddress. But I can't expand HomeAddress to see AddressType (which is also a class and have actual address properties).

    I tried this:
    [System.ComponentModel.TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
    public partial class AddressType
    {
    .......
    .......
    }

    [System.ComponentModel.TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
    public partial class Person {

    private AddressType homeAddressField;

    [System.ComponentModel.TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
    public AddressType HomeAddress
    {
                
        get {
                    return this.homeAddressField;
                }
            
        set {
                    this.homeAddressField = value;
                }
    }
            
    }



    But it doesn't work.........

    Please tell me how can I tell propertygrid to expand it when one class contains another class object.




    Sunday, December 2, 2007 4:44 AM

Answers

  • Hi

    Based on your post, you want to shown nested properties in the PropertyGrid like the Size of a control does, don’t you? If so, you need to custom your own TypeConverter for your AddressType and override the GetPropertiesSupported and GetProperties methods. I write a sample for your information.

    Code Block

    namespace PropertyGridP

    {

        public partial class NestedProperty : Form

        {

            public NestedProperty()

            {

                InitializeComponent();

            }

     

            private void NestedProperty_Load(object sender, EventArgs e)

            {

                Person p = new Person();

                p.HomeAddress.Country = "U.S.A.";

                p.HomeAddress.City = "New York";

                this.propertyGrid1.SelectedObject = p;

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                Person p = this.propertyGrid1.SelectedObject as Person;

                MessageBox.Show(p.HomeAddress.Country + "--" + p.HomeAddress.City);

            }

        }

     

        [TypeConverter(typeof(AddressTypeTypeConverter))]

        public class AddressType

        {

            string country;

            public string Country

            {

                get { return country; }

                set { country = value; }

            }

     

            string city;

            public string City

            {

                get { return city; }

                set { city = value; }

            }

        }

     

        public class Person

        {

            public Person()

            {

                homeAddressField = new AddressType();

            }

     

            private AddressType homeAddressField;

     

            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

            public AddressType HomeAddress

            {

                get { return this.homeAddressField; }

                set { this.homeAddressField = value; }

            }

        }

     

        public class AddressTypeTypeConverter : TypeConverter

        {

            public override object ConvertTo(ITypeDescriptorContext context,

                System.Globalization.CultureInfo culture, object value, Type destinationType)

            {//This method is used to shown information in the PropertyGrid.

                if (destinationType == typeof(string))

                {

                    return ((AddressType)value).Country + "," + ((AddressType)value).City;

                }

                return base.ConvertTo(context, culture, value, destinationType);

            }

     

            public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)

            {

                return TypeDescriptor.GetProperties(typeof(AddressType), attributes).Sort(new string[] { "Country", "City" });

            }

     

            public override bool GetPropertiesSupported(ITypeDescriptorContext context)

            {

                return true;

            }

        }

    }

     

    For more information about how to implement a Type Converter, please check the following article:

    ·         How to: Implement a Type Converter

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Thursday, December 6, 2007 3:06 AM

All replies

  • Hi

    Based on your post, you want to shown nested properties in the PropertyGrid like the Size of a control does, don’t you? If so, you need to custom your own TypeConverter for your AddressType and override the GetPropertiesSupported and GetProperties methods. I write a sample for your information.

    Code Block

    namespace PropertyGridP

    {

        public partial class NestedProperty : Form

        {

            public NestedProperty()

            {

                InitializeComponent();

            }

     

            private void NestedProperty_Load(object sender, EventArgs e)

            {

                Person p = new Person();

                p.HomeAddress.Country = "U.S.A.";

                p.HomeAddress.City = "New York";

                this.propertyGrid1.SelectedObject = p;

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                Person p = this.propertyGrid1.SelectedObject as Person;

                MessageBox.Show(p.HomeAddress.Country + "--" + p.HomeAddress.City);

            }

        }

     

        [TypeConverter(typeof(AddressTypeTypeConverter))]

        public class AddressType

        {

            string country;

            public string Country

            {

                get { return country; }

                set { country = value; }

            }

     

            string city;

            public string City

            {

                get { return city; }

                set { city = value; }

            }

        }

     

        public class Person

        {

            public Person()

            {

                homeAddressField = new AddressType();

            }

     

            private AddressType homeAddressField;

     

            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

            public AddressType HomeAddress

            {

                get { return this.homeAddressField; }

                set { this.homeAddressField = value; }

            }

        }

     

        public class AddressTypeTypeConverter : TypeConverter

        {

            public override object ConvertTo(ITypeDescriptorContext context,

                System.Globalization.CultureInfo culture, object value, Type destinationType)

            {//This method is used to shown information in the PropertyGrid.

                if (destinationType == typeof(string))

                {

                    return ((AddressType)value).Country + "," + ((AddressType)value).City;

                }

                return base.ConvertTo(context, culture, value, destinationType);

            }

     

            public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)

            {

                return TypeDescriptor.GetProperties(typeof(AddressType), attributes).Sort(new string[] { "Country", "City" });

            }

     

            public override bool GetPropertiesSupported(ITypeDescriptorContext context)

            {

                return true;

            }

        }

    }

     

    For more information about how to implement a Type Converter, please check the following article:

    ·         How to: Implement a Type Converter

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Thursday, December 6, 2007 3:06 AM
  • What if you wanted to display a tree-like interface to your Data instead? For example, lets say, the Data was organized such that People were organized by City first, then by Country. And in the PropertyGrid, you would like to see this organization, Country -> City -> Person.

     

    This would look similiar to how a tree control would display the information, but the PropertyGrid would allow the user to set the Properties of each Node as they traverse down.

    Friday, December 14, 2007 7:38 PM