Support for EditorBrowsableAttribute

Coordinator
Aug 26, 2009 at 10:44 AM

Received some positive feedback yesterday regarding honoring the EditorBrowsable.Never Attribute. Originally I felt this would not be necessary, as it typically is used for designers like Visual Studio or Blend, but as this property grid could be used for designer purposes, it makes sense to support this attribute. Thanks to Greg for this recommendation.

This is a simple mod to the PropertyGrid; under the ParseObject method, modify as follows...

static List<PropertyItem> ParseObject(object objItem)
{
	if (null == objItem)
		return new List<PropertyItem>();

	List<PropertyItem> pc = new List<PropertyItem>();
	Type t = objItem.GetType();
	var props = t.GetProperties();

	foreach (PropertyInfo pinfo in props)
	{
		bool isBrowsable = true;
		BrowsableAttribute b = PropertyItem.GetAttribute<BrowsableAttribute>(pinfo);
		if (null != b)
			isBrowsable = b.Browsable;
		// Added check for EditorBrowsableState.Never 8/26/2009
		if (isBrowsable)
		{
			EditorBrowsableAttribute eb = PropertyItem.GetAttribute<EditorBrowsableAttribute>(pinfo);
			if (null != eb && eb.State == EditorBrowsableState.Never)
				isBrowsable = false;
		}
		if (isBrowsable)
		{
			bool readOnly = false;
			ReadOnlyAttribute attr = PropertyItem.GetAttribute<ReadOnlyAttribute>(pinfo);
			if (attr != null)
				readOnly = attr.IsReadOnly;

			try
			{
				object value = pinfo.GetValue(objItem, null);
				PropertyItem prop = new PropertyItem(objItem, value, pinfo, readOnly);
				pc.Add(prop);
			}
			catch { }
		}
	}

	return pc;
}