I am defining a custom class to be used with the PropertyGrid control. Say, one of the properties is defined as such:
[CategoryAttribute("Section Name"),
DefaultValueAttribute("Default value"),
DescriptionAttribute("My property description")]
public string MyPropertyName
{
get { return _MyPropertyName; }
set { _MyPropertyName = value; }
}
private string _MyPropertyName;
As you see DefaultValueAttribute
defines a default value for the property. Such default value is used in two cases:
If this property value is changed from the default one the
PropertyGrid
control will display it in bold, andIf I call
ResetSelectedProperty
method of thePropertyGrid
, it will apply that default value to a selected cell.
This concept works fine, except one limitation of the DefaultValueAttribute
. It accepts only a constant value. So I'm curious, can I set it dynamically, say, from a constructor or later in the code?
EDIT: I was able to find this code that lets me read the DefaultValueAttribute
:
AttributeCollection attributes = TypeDescriptor.GetProperties(this)["MyPropertyName"].Attributes;
DefaultValueAttribute myAttribute = (DefaultValueAttribute)attributes[typeof(DefaultValueAttribute)];
string strDefaultValue = (string)myAttribute.Value;
The question is, how do you set it?