The WinForms PropertyGrid control has a way to use the DefaultValueAttribute to show certain values in bold when this value is different from the DefaultValueAttribute that has been set for this particular value.
We need to be able to change this DefaultValueAttribute at run-time to change these default values. I have tried this using reflection, to directly set the "value" field of this attribute, and inheriting the DefaultValueAttribute class in order to create a method to set the value to a new value (DefaultValueAttribute has a protected method SetValue(object value) that can be called). Neither of these methods work, even though, the new value is reflected by the attribute.Value property.
We're doing the exact same thing with the BrowsableAttribute (to show and hide settings in the PropertyGrid during run-time) and the ReadOnlyAttribute. Both of these work fine; the PropertyGrid gets updated to reflect the new attributes. For some reason the PropertyGrid completely ignores changes to the DefaultValueAttribute.
I have read that these values are 'fixed at compile time'. I am not sure I understand what that means, since this doesn't explain the behavior where the other attributes seem to work fine when changed during run-time.
Does anyone know what's different about the DefaultValueAttribute, and if there is a work-around to deal with this? (I know about the ShouldSerializeProperty() method. This doesn't work for our application, since the object is being serialized). We simply need to be able to set some 'default value' for each proptery in a class so that, whenever a property differs from that default value, the PropertyGrid draws this particular setting in bold.
Any help will be greatly appreciated!
Aucun commentaire:
Enregistrer un commentaire