4

我坚持使用 BindingList,其中 T 是扩展 A 接口的接口。当我在绑定中使用这个 bindingList 时,只有来自 T 的属性是可见的,而来自继承的 A 接口的属性不可见。为什么会这样?它看起来像一个 .net 错误。我需要我的 2 个项目来共享一些通用功能。当从 baseImplementation 传输 PropertyChanged 事件时,绑定列表的 PropertyDescriptor 也为空。附加的接口和实现。到底SetUp方法

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;
    }
4

1 回答 1

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 来解决此问题,但这两者都很棘手。老实说,数据绑定使用类比使用接口更容易。

于 2011-12-04T11:24:19.190 回答