C# 绑定列表<;T>;其中T是实现其他接口的接口
我一直使用BindingList,其中T是一个扩展接口的接口。在绑定中使用此bindingList时,只有来自T的属性可见,而来自继承的接口的属性不可见。为什么会这样?它看起来像一个.net错误。这是我的两个项目共享一些常见功能所必需的。当从baseImplementation隧道传输PropertyChanged事件时,绑定列表的PropertyDescriptor为空。 附加的接口和实现。最后介绍了安装方法C# 绑定列表<;T>;其中T是实现其他接口的接口,c#,interface,bindinglist,inherited,C#,Interface,Bindinglist,Inherited,我一直使用BindingList,其中T是一个扩展接口的接口。在绑定中使用此bindingList时,只有来自T的属性可见,而来自继承的接口的属性不可见。为什么会这样?它看起来像一个.net错误。这是我的两个项目共享一些常见功能所必需的。当从baseImplementation隧道传输PropertyChanged事件时,绑定列表的PropertyDescriptor为空。 附加的接口和实现。最后介绍了安装方法 interface IExtendedInterface : IBaseInterf
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;
}
接口IExtendedInterface:IBaseInterface
{
字符串C{get;}
}
接口IBASE接口:INotifyPropertyChanged
{
字符串A{get;}
字符串B{get;}
}
公共类基类实现:IBaseInterface
{
公共字符串A
{
获取{return“Base a”;}
}
公共字符串B
{
获取{return“base b”;}
保护集
{
B=数值;
不动产变更(“B”);
}
}
受保护的void OnPropertyChanged(字符串p)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的System.ComponentModel.PropertyChangedEventArgs(p));
}
公共事件System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
}
公共类扩展实现:BaseImplementation,IExtendedInterface
{
公共字符串C
{
获取{返回“扩展C”;}
}
}
私有void SetupData()
{
BindingList=新建BindingList();
添加(新的ExtendedImplementation());
添加(新的ExtendedImplementation());
dataGridView1.DataSource=列表;
}
属性是通过(间接)TypeDescriptor.GetProperties(typeof(T))获得的,但其行为与预期一致。接口的属性永远不会返回,即使是从基于类的模型返回,除非它们在该类型的公共API上(对于接口,这意味着在立即类型上)。类继承是不同的,因为这些成员仍然在公共API上。当一个接口:IsoOtherInterface时,即“实现”,而不是“继承”。举一个简单的例子,当这可能是一个问题时,考虑(完全合法):
现在;什么是IC.Foo
您可以通过为接口注册自定义TypeDescriptionProvider或使用ITypedList来绕过此问题,但这两种方法都很棘手。老实说,数据绑定在类中比在接口中更容易工作。尽量不要在文件中附加代码。我们不需要整个项目。将您的示例分解为尽可能小的程序,并将其粘贴为文本/代码。您所说的“不可见”是什么意思?如果您绑定到这些属性,它们可能会显示为不可访问,但这只是设计时绑定的结果,当您运行应用程序时,它们应该都正常。它们在绑定网格中不可见,当从绑定列表触发ItemChanged事件时,如果它涉及BaseInterface中的属性,则我得到null PropertyDescriptor。应用程序运行时它不工作。只需粘贴代码,然后查看。因为我的代表,我无法发布图片。谢谢你的解释。这将有助于将来避免绑定到接口的问题。
interface IA { int Foo {get;} }
interface IB { string Foo {get;} }
interface IC : IA, IB {}