I want to display multiple instances of one class in my PropertyGrid
. The class looks like this:
public class Parameter
{
[Description("the name")]
public string Name { get; set; }
[Description("the value"), ReadOnly(true)]
public string Value { get; set; }
[Description("the description")]
public string Description { get; set; }
}
I have many instances of that class in a TreeView
. When I select one of them in my TreeView
, the properties show up in the PropertyGrid
as expected. So far so good, but I want to customise this behaviour in the following way:
For each single instance I want to be able to prevent the user from modifying a specific property. By setting ReadOnly(true)
within my class (as you can see in the example above), all Value
properties will be disabled on a class-level.
After some research I found the following solution which gives me the opportunity to enable/disable a specific property at runtime:
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this)["Value"];
ReadOnlyAttribute attr =
(ReadOnlyAttribute)descriptor.Attributes[typeof(ReadOnlyAttribute)];
FieldInfo isReadOnly = attr.GetType().GetField(
"isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
isReadOnly.SetValue(attr, false);
This approach works just fine but unfortunately also on class-level only. This means if I set the Value
's isReadOnly
to false
, all of my Parameter
-objects have the Value
property writeable. But I want this ONLY on that one particular object (thus object-level). I really don't want to create separate classes for read/write and readonly properties.
As I am running out of ideas, your help is much appreciated :)
Thanks in advance!
EDIT: I need the readonly properties to be grayed-out, so the user can see that it's not allowed or possible to edit them.