绑定IList< IMyInterfaceType>不显示IMyInterface继承的Interfaces的成员 [英] Binding IList<IMyInterfaceType> doesn't display members of Interfaces that IMyInterface inherits

查看:185
本文介绍了绑定IList< IMyInterfaceType>不显示IMyInterface继承的Interfaces的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将IList绑定到GridView。 IMyInterface看起来像

  public interface IMyInterface:IHasTotalHours,IHasLines 
{
DateTime GoalStartDate {get;组; }
DateTime GoalEndDate {get;组;
}

我将一个实例绑定到这样的网格:

  IList< IMyInterface> instance = GetMyData(); 

myGrid.DataSource = instance;
myGrid.DataBind();

当绑定到网格时,网格中显示的唯一成员是IMyInterface:GoalStartDate和GoalEndDate。



为什么?如何让网格显示其继承的其他接口的成员?



更新
继承的接口定义简单数据属性如

  public interface IHasTotalHours 
{
string描述{get;组; }
int小时{get;组;
}
public interface IHasLines
{
double TotalLines {get;组; }
double LinesPerHour {get;组;
}

有一个类实现IMyInterface:

  public class MyClass:IMyInterface 
{
public string Description {get;组; }
public int Hours {get;组; }
public double TotalLines {get;组; }
public double LinesPerHour {get;组; }
public DateTime GoalStartDate {get;组; }
public DateTime GoalEndDate {get;组; }

}

这些被转换为IMyInterface,并在列表中返回我绑定到GridView。

解决方案

数据绑定控件不使用反射,而是从TypeDescriptor获取属性数据源。在 TypeDescriptor.GetProperties 方法中,您可以阅读以下内容:


组件的属性可以
与类的属性不同,
,因为该站点可以添加或删除
属性,如果组件位于。


显然,默认实现只会从接口返回直接属性,而不是继承幸运的是,这种机制是可扩展的,你可以写一个 TypeConverter 具有自定义属性信息实现的类。请参阅TypeConverter文档中的实现属性逻辑的​​说明。



您的自定义TypeConverter类的GetProperties实现可以在您的界面上调用TypeDescriptor.GetProperties(Type)它是继承的接口。但是也许你甚至可以编写一个通用的TypeConverter,它可以通过使用反射来找到所有继承的属性。



然后你使用TypeConverterAttribute属性将这个自定义的TypeConverter附加到你的接口。 p>

然后,像魔术一样,数据源会找到所有属性。 ; - )


I'm binding IList to a GridView. IMyInterface looks like

public interface IMyInterface: IHasTotalHours, IHasLines
{
    DateTime GoalStartDate { get; set; }
    DateTime GoalEndDate { get; set; }
}

I bind an instance to a Grid like this:

IList<IMyInterface> instance= GetMyData();

myGrid.DataSource = instance;
myGrid.DataBind();

When bind this to the grid, the only members that show up in the grid are the direct members of IMyInterface: GoalStartDate and GoalEndDate.

Why is that? How do I get the grid to display the members of the other interfaces it inherits?

Update The inherited interfaces define simple data properties like

public interface IHasTotalHours
{
    string Description { get; set; }
    int Hours{ get; set; }
}
public interface IHasLines
{
    double TotalLines { get; set; }
    double LinesPerHour { get; set; }
}

There is a class that implements IMyInterface:

public class MyClass : IMyInterface
{
    public string Description { get; set; }
    public int Hours { get; set; }
    public double TotalLines { get; set; }
    public double LinesPerHour { get; set; }
    public DateTime GoalStartDate { get; set; }
    public DateTime GoalEndDate { get; set; }

}

These are cast as IMyInterface, and returned in the list that I'm binding to the GridView.

解决方案

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. ;-)

这篇关于绑定IList&lt; IMyInterfaceType&gt;不显示IMyInterface继承的Interfaces的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆