Click here to Skip to main content
15,897,166 members
Articles / Programming Languages / C#

Extending the PropertyGrid with a new PropertyTab

Rate me:
Please Sign up or sign in to vote.
4.89/5 (39 votes)
15 Jan 2007CPOL8 min read 121.3K   4.1K   128  
Add a PropertyTab showing the fields of an object and overlay icons to the PropertyGrid
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Collections;

namespace href.Controls.PropGridEx
{

    /// <summary>
    /// PropertyDescriptor for all items in an IDictionary
    /// </summary>
    internal class DictionaryItemMemberDescriptor : PropertyDescriptor
    {
        private IDictionary m_Dict;
        private object m_Key;

        internal DictionaryItemMemberDescriptor(IDictionary list, object key)
            :
            base(String.Format("[{0}]", key), null)
        {
            this.m_Dict = list;
            this.m_Key = key;
        }

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

        public override Type ComponentType
        {
            get { return typeof(IDictionary); }
        }

        public override object GetValue(object component)
        {
            return this.m_Dict[this.m_Key];
        }

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

        public override Type PropertyType
        {
            get
            {
                object val = this.m_Dict[this.m_Key];
                if (val != null)
                    return val.GetType();
                else
                    return typeof(object);
            }
        }

        public override void ResetValue(object component)
        {

        }

        public override void SetValue(object component, object value)
        {

        }

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


        /// <summary>
        /// Create an AttributeCollection that ensures an ExpandableObjectConverter
        /// and add Description and Category attributes
        /// </summary>
        public override AttributeCollection Attributes
        {
            get
            {
                bool hasExpandebleTypeConverter = false;
                AttributeCollection baseAttribs = base.Attributes;
                List<Attribute> attributes = new List<Attribute>(baseAttribs.Count);
                foreach (Attribute baseAttribute in baseAttribs)
                {
                    TypeConverterAttribute tca = baseAttribute as TypeConverterAttribute;

                    if ( (tca != null) && (Type.GetType( tca.ConverterTypeName).IsSubclassOf(typeof(ExpandableObjectConverter)) ) )
                    {
                        attributes.Add(baseAttribute);
                        hasExpandebleTypeConverter = true;
                    
                    }
                    else
                    {
                        attributes.Add(baseAttribute);
                    }
                }


                string category = "IDictionary";

                attributes.Add(new CategoryAttribute(category));

                attributes.Add(new DescriptionAttribute(String.Format("Dictionary item with key '{0}'", this.m_Key)));


                // add expandable type conv?
                if (this.m_Key != null)
                {
                    object value = this.m_Dict[ this.m_Key];
                    if (value != null)
                    {
                        Type valueType = value.GetType();
                        // add expandable type conv?
                        if ((!hasExpandebleTypeConverter) && (!valueType.IsValueType) && (valueType != typeof(string)))
                        {
                            attributes.Add(new TypeConverterAttribute(typeof(ExpandableObjectConverter)));
                        }
                    }
                }
                AttributeCollection result = new AttributeCollection(attributes.ToArray());



                return result;
            }
        }
    }
   
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Germany Germany
Carsten started programming Basic and Assembler back in the 80’s when he got his first C64. After switching to a x86 based system he started programming in Pascal and C. He started Windows programming with the arrival of Windows 3.0. After working for various internet companies developing a linguistic text analysis and classification software for 25hours communications he is now working as a contractor.

Carsten lives in Hamburg, Germany with his wife and five children.

Comments and Discussions