Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Windows Forms » Downloads
 
Add your own
alternative version

Unleash PropertyGrid with Dynamic Properties and Globalization

, 29 Jan 2004 CPOL
The article presents a way to enhance the use of the PropertyGid control with dynamic properties and globalization
using System;
using System.ComponentModel;
using System.Globalization;
using System.Resources;
using System.Reflection;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Windows.Forms;
using CustomControls.HelperClasses;

	
namespace CustomControls.ApplicationBlocks
{
	public   class DynamicTypeDescriptor : ICustomTypeDescriptor , ISupportInitialize
	{
		private PropertyDescriptorCollection dynamicProps;
		private PropertyCommands _PropertyCommands;
		private CategoryCommands _CategoryCommands;
		
		

		 
		[Category ("Property Control")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		[Editor(typeof(CustomCommandsCollectionEditor),typeof(System.Drawing.Design.UITypeEditor))]
		public PropertyCommands PropertyCommands
		{
			get
			{
				if(_PropertyCommands==null)
				{
					_PropertyCommands= new PropertyCommands();	
				}
				return _PropertyCommands;
			}
		}

		
		[Category ("Property Control")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		[Editor(typeof(CustomCommandsCollectionEditor),typeof(System.Drawing.Design.UITypeEditor))]
		public CategoryCommands CategoryCommands 
		{
			get
			{
				if(_CategoryCommands==null)
				{
					_CategoryCommands= new CategoryCommands();
				}
				return _CategoryCommands;
			}
		}

				

		public DynamicTypeDescriptor()
		{
		
		}
	
		public bool ShouldSerializePropertyCommands(){return true;}

		public bool ShouldSerializeCategoryCommands(){return true;}


		

		protected void FillPropCommColl()
		{
			PropertyDescriptorCollection baseProps = TypeDescriptor.GetProperties(this,true);

			foreach( PropertyDescriptor oProp in baseProps )
			{			
				if(!oProp.DesignTimeOnly && oProp.IsBrowsable  &&  oProp.Category!="Property Control")
				{
					PropertyCommand pc= new PropertyCommand(oProp.Name,oProp.IsBrowsable, oProp.IsReadOnly);
					PropertyCommands.Add(pc);		
				}
			}
			PropertyCommands.Sort();
					
		}

		protected void FillCatCommColl()
		{
			PropertyDescriptorCollection baseProps = TypeDescriptor.GetProperties(this,true);

			foreach( PropertyDescriptor oProp in baseProps )
			{			
				if(!oProp.DesignTimeOnly && oProp.IsBrowsable &&  oProp.Category!="Property Control")
				{
					CategoryCommand cc= new CategoryCommand(oProp.Category, true);
					if(CategoryCommands.BinarySearch(cc)<0 )
					{
						CategoryCommands.Add(cc);
					}
				}
			}
			CategoryCommands.Sort();
					
		}
	
		public virtual void BeginInit()
		{
			
		}
		public virtual void EndInit()
		{
			if(PropertyCommands.Count==0){FillPropCommColl();}
			if(CategoryCommands.Count==0){FillCatCommColl();}
		}

	
		public  PropertyCommand[] GetPropertyCommands()
		{
			PropertyDescriptorCollection baseProps = TypeDescriptor.GetProperties(this,true);
			PropertyCommand[] pcs= new PropertyCommand[baseProps.Count];
			
			for (int i=0;i<baseProps.Count;i++)
			{
				pcs[i]=new PropertyCommand(baseProps[i].Name,baseProps[i].IsBrowsable,baseProps[i].IsReadOnly);
			}
			return pcs;
		}


		public virtual  string GetLocalizedName(string Name)
		{
			return Name;
		}
		
		public virtual string GetLocalizedDescription(string Description)
		{
			return Description;
		}

		
		#region "TypeDescriptor Implementation"
	
		public String GetClassName()
		{
			return TypeDescriptor.GetClassName(this,true);
		}

		public AttributeCollection GetAttributes()
		{
			return TypeDescriptor.GetAttributes(this,true);
		}

		public String GetComponentName()
		{
			return TypeDescriptor.GetComponentName(this, true);
		}

		public TypeConverter GetConverter()
		{
			return TypeDescriptor.GetConverter(this, true);
		}

		public EventDescriptor GetDefaultEvent() 
		{
			return TypeDescriptor.GetDefaultEvent(this, true);
		}

		public PropertyDescriptor GetDefaultProperty() 
		{
			return TypeDescriptor.GetDefaultProperty(this, true);
		}

		public object GetEditor(Type editorBaseType) 
		{
			return TypeDescriptor.GetEditor(this, editorBaseType, true);
		}

		public EventDescriptorCollection GetEvents(Attribute[] attributes) 
		{
			return TypeDescriptor.GetEvents(this, attributes, true);
		}

		public EventDescriptorCollection GetEvents()
		{
			return TypeDescriptor.GetEvents(this, true);
		}

		public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
		{
		 
			if (!CustomControls.Functions.General.IsInDesignMode() ) 
			{
				PropertyDescriptorCollection baseProps = TypeDescriptor.GetProperties(this, attributes, true);
				dynamicProps = new PropertyDescriptorCollection(null);

				foreach( PropertyDescriptor oProp in baseProps )
				{
		
					if( oProp.Category!="Property Control")
					{
						if( (PropertyCommands[oProp.Name] ==null || PropertyCommands[oProp.Name].Visible) &&  (CategoryCommands[oProp.Category] ==null || CategoryCommands[oProp.Category].Visible))
						{
							dynamicProps.Add(new DynamicPropertyDescriptor(this,oProp));
						}
					}
				}
				return dynamicProps;
			}

			return TypeDescriptor.GetProperties(this, attributes, true);
			
		}

		public PropertyDescriptorCollection GetProperties()
		{
			
			if ( dynamicProps == null) 
			{
				PropertyDescriptorCollection baseProps = TypeDescriptor.GetProperties(this, true);
				dynamicProps = new PropertyDescriptorCollection(null);

				foreach( PropertyDescriptor oProp in baseProps )
				{			
				
					dynamicProps.Add(new DynamicPropertyDescriptor(this,oProp));
					
				}
			}
			return dynamicProps;
		}

		public object GetPropertyOwner(PropertyDescriptor pd) 
		{
			return this;
		}
		#endregion
	}


	public class DynamicPropertyDescriptor : PropertyDescriptor
	{
		private PropertyDescriptor basePropertyDescriptor; 
		private DynamicTypeDescriptor instance;

		public DynamicPropertyDescriptor(DynamicTypeDescriptor instance,PropertyDescriptor basePropertyDescriptor) : base(basePropertyDescriptor)
		{
			this.instance=instance;
			this.basePropertyDescriptor = basePropertyDescriptor;
		}

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

		public override Type ComponentType
		{
			get { return basePropertyDescriptor.ComponentType; }
		}

		public override object GetValue(object component)
		{
			return this.basePropertyDescriptor.GetValue(component);
		}

		public override string Description
		{
			get
			{
				return instance.GetLocalizedDescription(base.Name);
			}
		}
		public override string Category
		{
			get
			{
				return instance.GetLocalizedName(base.Category);
			}
		}

		public override string DisplayName
		{
			get
			{
				return instance.GetLocalizedName(base.Name);
			}
		}

		public override bool IsReadOnly
		{
			get
			{
				PropertyCommand pc=instance.PropertyCommands[this.Name] as PropertyCommand;
				if(pc!=null)
				{
					return pc.ReadOnly;
				}
				else
				{
					return this.basePropertyDescriptor.IsReadOnly; 
				}
			}
		}

		public override Type PropertyType
		{
			get { return this.basePropertyDescriptor.PropertyType; }
		}

		public override void ResetValue(object component)
		{
			this.basePropertyDescriptor.ResetValue(component);
		}

		public override bool ShouldSerializeValue(object component)
		{
			return this.basePropertyDescriptor.ShouldSerializeValue(component);
		}

		public override void SetValue(object component, object value)
		{
			this.basePropertyDescriptor.SetValue(component, value);
		}
	}


}

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)

Share

About the Author

Daniel Zaharia
Web Developer
Romania Romania
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 30 Jan 2004
Article Copyright 2004 by Daniel Zaharia
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid