Binding IList<IMyInterfaceType> doesn't display members of Interfaces that IMyInterface inherits

◇◆丶佛笑我妖孽 提交于 2019-12-02 23:13:18

Data bound controls do not use reflection but a TypeDescriptor to get the properties from a data source. In the TypeDescriptor.GetProperties method, you can read the following:

The properties for a component can differ from the properties of a class, because the site can add or remove properties if the component is sited.

Apparently the default implementation will only return direct properties from an Interface and not the inherited ones.

Luckily this mechanism is extensible, and you can write a TypeConverter class with custom property information implementation. Please refer to the remarks in the TypeConverter documentation for implementing property logic.

The GetProperties implementation of your custom TypeConverter class can call TypeDescriptor.GetProperties(Type) on your interface and all it's inherited interfaces. But maybe you could even write a generic TypeConverter that would find all inherited properties by using reflection.

Then you attach this custom TypeConverter to your interface with the TypeConverterAttribute attribute.

And then, like magic, the data source will find all properties. ;-)

It's because an interface is a contract, and that's the only way to interact with an object is through that specific contract. The other interfaces cannot be assumed and can't be utilized until a cast is made.

So when you bind a List of T to something, the datagrid doesn't know about those other interfaces. And the datagrid isn't going to use reflection to figure out what other classes or interfaces might be inherited. The only object properties that are going to be available to the datagrid are the properties exposed by the T interface.

You need to bind List if you want the datagrid to have access to all the properties.

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