2011-12-04 53 views
4

我被BindingList卡住,其中T是扩展A接口的接口。当我在绑定中使用这个bindingList时,只有来自T的属性是可见的,而来自继承的A接口的属性则不是。为什么会发生?它看起来像一个.net错误。这我需要我的2个项目共享一些通用功能。当PropertyChanged事件从baseImplementation隧道传输时,绑定List的PropertyDescriptor也是空的。 附加接口和实现。到底BindingList <T>其中T是实现其他接口的接口

interface IExtendedInterface : IBaseInterface 
{ 
    string C { get; } 
} 

interface IBaseInterface : INotifyPropertyChanged 
{ 
    string A { get; } 
    string B { get; } 
} 

public class BaseImplementation : IBaseInterface 
{ 
    public string A 
    { 
     get { return "Base a"; } 
    } 

    public string B 
    { 
     get { return "base b"; } 
     protected set 
     { 
      B = value; 
      OnPropertyChanged("B"); 
     } 
    } 

    protected void OnPropertyChanged(string p) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(p)); 
    } 

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; 
} 

public class ExtendedImplementation : BaseImplementation, IExtendedInterface 
{ 
    public string C 
    { 
     get { return "Extended C"; } 
    } 
} 

private void SetupData() 
    { 
     BindingList<IExtendedInterface> list = new BindingList<IExtendedInterface>(); 
     list.Add(new ExtendedImplementation()); 
     list.Add(new ExtendedImplementation()); 
     dataGridView1.DataSource = list; 
    } 
+1

尽量不要在文件中附加代码。我们不需要整个项目。将你的例子分解成最小的程序,并将其作为文本/代码粘贴。 –

+0

“不可见”是什么意思?如果你绑定了这些属性,它们可能会显示为不可访问,但这只是设计时绑定的结果,当你运行应用程序时,它应该都可以。 –

+0

它们在绑定的网格中不可见,当从绑定列表触发ItemChanged事件时,如果它从BaseInterface处理属性,那么我得到null PropertyDescriptor。它在应用程序运行时不起作用。只需粘贴代码并查看。由于我的代表我不能发布图像 – user1079952

回答

6

的属性经由(间接)TypeDescriptor.GetProperties(typeof运算(T))获得,但是行为是按预期设置方法。来自接口的属性是从来没有返回,即使是基于类的模型,除非它们在该类型的公共API上(对于接口而言,意味着在直接类型上)。类继承是不同的,因为这些成员仍然在公共API上。当一个接口:ISomeOtherInterface,即“实现”,而不是“继承”。举一个简单的例子,说明这可能是一个问题,请考虑(完全合法):

interface IA { int Foo {get;} } 
interface IB { string Foo {get;} } 
interface IC : IA, IB {} 

现在;什么是IC.Foo?

可能能够通过为接口注册一个自定义的TypeDescriptionProvider或使用ITypedList来解决这个问题,但这两者都很棘手。说实话,数据绑定只是简单地使用类而不是接口。

+0

感谢您的解释。这将有助于未来避免绑定到接口的问题。 – user1079952

相关问题