Most of the programmers who uses Visual Studio .NET knows PropertyGrid which is the main control in the Properties Window of the Visual Studio.
PropertyGrid’s default PropertyTab (System.Windows.Forms.PropertyGridInternal.PropertiesTab) shows properties and their current values of the given object. If we want to make some customizations we can use Browsable, DefaultValue, Description, Category, Localizable etc. attributes. For example you can hide a Property by adding Localizable(false) attribute to that property.
If you want to make more customization which you cannot do with these attributes you can create your own PropertyTab and add it to the PropertyGrid. I made an example which filters properties according to my custom attribute(OzcanPropertyAttribute). Withal it shows hidden properties as a read-only property in the PropertyGrid.
To create you custom PropertyTab for the PropertyGrid, you have to derive you class from PropertyTab class. Then you have to override its GetProperties method.
You can add your custom PropertyTab to your PropertyGrid like;
private class CustomPropertyTab : PropertyTab
{
public CustomPropertyTab()
{ }
public override PropertyDescriptorCollection GetProperties(object component,
Attribute[] attributes)
{
PropertyInfo[] properties = component.GetType().GetProperties(
BindingFlags.Instance | BindingFlags.Public);
List filtereds = new List();
PropertyDescriptorCollection propertyDescriptions = TypeDescriptor.GetProperties(
component, attributes);
Type myAttributeType = typeof(OzcanPropertyAttribute);
List ignoreds = new List();
for (int i = 0; i < properties.Length; i++)
{
if (properties[i].IsDefined(myAttributeType, true))
{
filtereds.Add(propertyDescriptions[properties[i].Name]);
}
else
{
ignoreds.Add(properties[i].Name);
}
}
// now add a custom property descriptor to show hidden properties
PropertyDescriptorCollection coll = new PropertyDescriptorCollection(
filtereds.ToArray());
if (FormMain.Instance._Settings.ShowHiddenProperties)
coll.Add(new CustomPropertyDescriptor(ignoreds.ToArray()));
return coll;
}
public override Bitmap Bitmap
{
get
{
return Properties.Resources.finance_16;
}
}
public override string TabName
{
get { return "Ozcan Properties Tab"; }
}
}
If you want to make more customization you can create your own PropertyDescriptors and create your own PropertyDescriptorCollection which contains these PropertyDescriptors. Check example source codes for how to implement custom PropertyDescriptors.