Click here to Skip to main content
15,885,278 members
Articles / Desktop Programming / Windows Forms

Add Custom Properties to a PropertyGrid

Rate me:
Please Sign up or sign in to vote.
4.94/5 (219 votes)
22 Aug 2006Apache6 min read 1.6M   27.8K   443  
Extend a PropertyGrid with an Item collection; easy customization of properties with custom editor, custom converter and databinding.
namespace PropertyGridEx
{
    using System;
    using System.ComponentModel;
    using System.Globalization;
    using System.Runtime.CompilerServices;

    public class BrowsableTypeConverter : ExpandableObjectConverter
	{			
		public enum LabelStyle
		{
			lsNormal,
			lsTypeName,
			lsEllipsis
		}
		
		public class BrowsableLabelStyleAttribute : Attribute
		{
			
			private LabelStyle eLabelStyle = LabelStyle.lsNormal;
			public BrowsableLabelStyleAttribute(LabelStyle LabelStyle)
			{
				eLabelStyle = LabelStyle;
			}
			public LabelStyle LabelStyle
			{
				get
				{
					return eLabelStyle;
				}
				set
				{
					eLabelStyle = value;
				}
			}
		}
		
		public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type destinationType)
		{
			return true;
		}
		
		public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
		{
            BrowsableLabelStyleAttribute attribute1 = (BrowsableLabelStyleAttribute)context.PropertyDescriptor.Attributes[typeof(BrowsableLabelStyleAttribute)];
            if (attribute1 != null)
            {
                switch (attribute1.LabelStyle)
                {
                    case LabelStyle.lsNormal:
                        {
                            return base.ConvertTo(context, culture, RuntimeHelpers.GetObjectValue(value), destinationType);
                        }
                    case LabelStyle.lsTypeName:
                        {
                            return ("(" + value.GetType().Name + ")");
                        }
                    case LabelStyle.lsEllipsis:
                        {
                            return "(...)";
                        }
                }
            }
            return base.ConvertTo(context, culture, RuntimeHelpers.GetObjectValue(value), destinationType);
        }
		
	}
}

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 Apache License, Version 2.0


Written By
Software Developer (Senior)
Italy Italy
I am 40 years old and I've been working with C++, Visual Basic .NET, C# and ASP.NET. I have a large experience in Industrial Automation solutions, but I've worked also as Web developer and DBA. I like to share knowledge and projects with other people.

Comments and Discussions