Live Chat Icon For mobile
Live Chat Icon

From within a PropertyGrid, is there any way to display property names that differ from the actual class property names

Platform: WinForms| Category: Type Editors

You can do this but it’s not as simple as using an attribute. If you want to modify the property names for your component, you have to interact with the reflection mechanism that the grid is using by implementing
ICustomTypeDescriptor. The grid operates on objects called PropertyDescriptors with are the Framework’s generic wrapper for a property, where reflection is a specific implementation. So what ICustomTypeDescriptor allows you to do is to have *your object* asked for it’s properties rather than than the default system, which is reflection.

You then write your own derived version of PropertyDescriptor (an abstract class in System.ComponentModel) and return your property descriptors instead of the ones that come back from the TypeDescriptor… so some of the impl
would look something like this.

	public class MyFriendlyNamePropertyDescriptor : PropertyDescriptor 
	{ 
		private PropertyDescriptor baseProp; 
		private string friendlyName; 
		public MyFriendlyNamePropertyDescriptor(PropertyDescriptor baseProp, Attribute[] filter) 
			: base(baseProp)
		{ 
			this.baseProp = baseProp; 
		} 
		
		public override string Name 
		{ 
			get{return this.baseProp.Name;} 
		} 

		public override string DisplayName
		{ 
			get 
			{ 
				return GetFriendlyname(baseProp.Name); //replace with code to return a friendly name
			} 
		} 

		public override bool IsReadOnly 
		{ 
			get {return baseProp.IsReadOnly;} 
		} 
		
		public override bool CanResetValue(object component)
		{
			return this.baseProp.CanResetValue(component);
		}
		
		public override Type ComponentType
		{
			get{return baseProp.ComponentType;}
		}

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

		public override Type PropertyType
		{
			get{return this.baseProp.PropertyType;}
		}
		public override void ResetValue(object component)
		{
			baseProp.ResetValue(component);
		}
		public override void SetValue(object component, object Value)
		{
			this.baseProp.SetValue(component, Value);
		}
		public override bool ShouldSerializeValue(object component)
		{
			return this.baseProp.ShouldSerializeValue(component);
		}


	}

	public class MyClass : ICustomTypeDescriptor
	{

		PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] filter) 
		{ 
			PropertyDescriptorCollection baseProps = TypeDescriptor.GetProperties(GetType(), filter); 
			// notice we use the type here so we don’t recurse 
			PropertyDescriptor[] newProps = new PropertyDescriptor[baseProps.Count]; 
			for (int i = 0; i < baseProps.Count; i++) 
			{ 
				newProps[i] = new MyFriendlyNamePropertyDescriptor(baseProps[i], filter);
				string oldname = ((PropertyDescriptor)baseProps[i]).DisplayName ;
				string newname = ((MyFriendlyNamePropertyDescriptor)newProps[i]).DisplayName;
			} 
			// probably wanna cache this... 
			return new PropertyDescriptorCollection(newProps); 
		} 
 
		AttributeCollection ICustomTypeDescriptor.GetAttributes() 
		{ 
			return TypeDescriptor.GetAttributes(this, true); 
		} 
 
		string ICustomTypeDescriptor.GetClassName()
		{
			return TypeDescriptor.GetClassName(this, true);
		}
		
		string ICustomTypeDescriptor.GetComponentName()
		{
			return TypeDescriptor.GetComponentName(this, true);
		}

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

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

		EventDescriptorCollection ICustomTypeDescriptor.GetEvents(System.Attribute[] attributes)
		{
			return TypeDescriptor.GetEvents(this, attributes, true);
		}
		EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
		{
			return TypeDescriptor.GetEvents(this, true);
		}
		PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
		{
			return TypeDescriptor.GetDefaultProperty(this, true);
		}

		PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
		{
			return TypeDescriptor.GetProperties(this, true);
		}

		object ICustomTypeDescriptor.GetEditor(System.Type editorBaseType)
		{
			return TypeDescriptor.GetEditor(this, editorBaseType, true);
		}
		object ICustomTypeDescriptor.GetPropertyOwner(System.ComponentModel.PropertyDescriptor pd)
		{
			return this;
		}



		// the code for MyClass.... including the properties to show in the propertygrid

	}

(originally from sburke_online@microsoft..nospam..com on microsoft.public.dotnet.framework.windowsforms. This code suggested by Rachid El Masoudi in an email to winformsfaq@syncfusion.com)

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.