Expand custom object in property grid without any modifications to the class?

最后都变了- 提交于 2019-12-24 00:46:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!