问题
By default, custom objects are grayed out, and not expandable:

I know it's possible to make them expandable using ExpandableObjectConverter, but that requires extending the original class.
What if I cannot modify the original class? Is there a generic approach of handling custom object expansion inside a property grid?
回答1:
You can dynamically add the TypeConverterAttribute
for the ExpandableObjectConverter
at runtime, using something like the following (VB.NET).
Dim attr = New TypeConverterAttribute(GetType(ExpandableObjectConverter))
TypeDescriptor.AddAttributes(GetType(ExtensionDataObject), attr)
You would need to AddAttribute
for each type you want to be expandable. If the types are all in a specific namespace, you could use reflection to find them:
Dim assm = Assembly.GetExecutingAssembly() ' or some other assembly
For Each t In assm.GetTypes().Where(Function(x) x.Namespace = "InterestingTypes")
TypeDescriptor.AddAttributes(t, attr)
Next
(sorry for the broken syntax highlighting - I should have used C#!)
来源:https://stackoverflow.com/questions/24151066/expand-custom-object-in-property-grid-without-any-modifications-to-the-class