Hide some properties in PropertyGrid at run-time
I believe you are looking for custom type descriptors.
While the other answer is sharing correct information about Browsable
attribute and BrowsableAttributes
of PropertyGrid
, but I'd say it's not a proper practical solution for the problem.
It's not practical to set Browsable
attribute, or any other custom attributes for existing control classes like Label
, Button
, and so on. Because in this way, the op needs to override all properties of those classes and decorate them with suitable attribute. And even worst, not all propertied are overridable.
What's the practical solution?
As I mentioned earlier, I believe you are looking for custom type descriptors. You can provide metadata about an object assigning a new TypeDescriptor
or implementing ICustomTypeDescriptor
or deriving from CustomTypeDescriptor
.
Example
Here for example, I create a CustomObjectWrapper
class deriving from CustomTypeDescriptor
which accepts an object in constructor. This way I can simply filter the properties of the wrapped object by overriding GetProperties
.
Then instead of assigning button1
to PropertyGrid
, I wrap it in CustomObjectWrapper
and assing the CustomObjectWrapper
to property grid. This way it just shows the filtered properties and the properties are actually come from button1
.
Here is the implantation:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
public class CustomObjectWrapper : CustomTypeDescriptor
{
public object WrappedObject { get; private set; }
public List<string> BrowsableProperties { get; private set; }
public CustomObjectWrapper(object o)
:base(TypeDescriptor.GetProvider(o).GetTypeDescriptor(o))
{
WrappedObject = o;
BrowsableProperties = new List<string>() { "Text", "BackColor" };
}
public override PropertyDescriptorCollection GetProperties()
{
return this.GetProperties(new Attribute[] { });
}
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
var properties = base.GetProperties(attributes).Cast<PropertyDescriptor>()
.Where(p=>BrowsableProperties.Contains(p.Name))
.Select(p => TypeDescriptor.CreateProperty(
WrappedObject.GetType(),
p,
p.Attributes.Cast<Attribute>().ToArray()))
.ToArray();
return new PropertyDescriptorCollection(properties);
}
}
And as usage:
propertyGrid1.SelectedObject = new CustomObjectWrapper(button1);
You can simply add new property names to BrowsableProperties
of the CustomObjectWrapper
. It's a public property.
UPDATE
Please note this is only useful for Hiding properties (when you can). Reza Aghaei answer is actually the correct answer.
I'll leave this here as it's suitable for the other case when you just simply want to hide a property when you have access to it.
Original
Easiest way is probably to use
[Browsable(false)]
BrowsableAttribute Class
Specifies whether a property or event should be displayed in a Properties window.
[Browsable(false)]
public int SecretSquirrels
{
get; set;
}
Also as pointed out by Marc Gravell, there is also
PropertyGrid.BrowsableAttributes Property
Gets or sets the browsable attributes associated with the object that the property grid is attached to.