This project has moved. For the latest updates, please go here.

ComplexObject doesnt show corresponding properties in property Grid

Jul 19, 2012 at 2:31 PM

Hi there,

I have collection of objects; say collection of Student which I would like to display in the property grid. I have achieved displaying the collection and I am able to expand it as well. But unfortunately, when I expand I can just see the Class' name (Student) in the property grid. What I would like to see is Student Object displaying all the properties so that I can edit the values at run time. I have done this before with WPG but the same thing here fails... Can any one please give me a hint to proceed?

        [Category("Parameter Specific Properties")]
        [Description("Parameter's Details")]
        [DisplayName("Parameter's Details")]
        [TypeConverter(typeof(ParameterConverter))]
        [ExpandableObject]
        public ParameterCollection ParamsDetails
        {
            get { return _ParamsDetails; }
            set { _ParamsDetails = value; }
        }


    public class ParameterCollection : ObservableCollection<Parameter>, ICustomTypeDescriptor
    {
	// here I have code to add and remove params..
    }

    public class Parameter : INotifyPropertyChanged
    {
	//I have few properties here...
    }


    public class ParameterConverter : ExpandableObjectConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType)
        {
            if (destType == typeof(string) && value is ParameterCollection)
            {

                return "Default data";
            }
            return base.ConvertTo(context, culture, value, destType);
        }
    }

    public class ParameterCollectionPropertyDescriptor : PropertyDescriptor
    {
        private ParameterCollection collection = null;
        private int index = -1;

        public ParameterCollectionPropertyDescriptor(ParameterCollection coll, int idx) :
            base(Common.ConvertStringToAlphaNumericUnderscore(idx.ToString()), null)
        {
            this.collection = coll;
            this.index = idx;
        }

        public override AttributeCollection Attributes
        {
            get
            {
                return new AttributeCollection(null);
            }
        }

        public override bool CanResetValue(object component)
        {
            return true;
        }

        public override Type ComponentType
        {
            get
            {
                return this.collection.GetType();
            }
        }

        public override string DisplayName
        {
            get
            {
                return "_" + this.collection[index].Name;
            }
        }

        public override string Description
        {
            get
            {
                return this.collection[index].Name;
            }
        }

        public override object GetValue(object component)
        {
            return this.collection[index];
        }

        public override bool IsReadOnly
        {
            get { return false; }
        }

        public override string Name
        {
            get { return Common.ConvertStringToAlphaNumericUnderscore(index.ToString()); }
        }

        public override Type PropertyType
        {
            get { return this.collection[index].GetType(); }
        }

        public override void ResetValue(object component)
        {
        }

        public override bool ShouldSerializeValue(object component)
        {
            return true;
        }

        public override void SetValue(object component, object value)
        {
            //this.collection[index] = value;
        }
    }

    public static class Common
    {
        public static string ConvertStringToAlphaNumericUnderscore(string input)
        {
            Regex onlyAlphaNumUscore = new Regex("[^a-zA-Z0-9_]");
            Regex onlyStartingDigits = new Regex("^[0-9]+");

            string result = onlyAlphaNumUscore.Replace(input, "_");

            if (onlyStartingDigits.IsMatch(result))
                result = "_" + result;

            return result;
        }
    }

 

Thanks in advance...