【问题标题】:Why BindingSource component cannot see inherited properties?为什么 BindingSource 组件看不到继承的属性?
【发布时间】:2010-11-13 01:01:50
【问题描述】:

我已经定义了类:

public class Parent : IParent
{
    public string ParentText
    {
        get { return "ParentText"; }
    }
}

public interface IParent
{
    string ParentText { get;}
}

public class Child : Parent, IChild
{
    public string ChildText
    {
        get { return "ChildText"; }
    }
}

public interface IChild : IParent
{
    string ChildText { get;}
}

当我尝试将控件绑定到 IChild 实例时,我可以为 ChildText 属性执行此操作,但不能为 ParentText 属性执行此操作。如果我尝试绑定到 Child 实例,这两个属性都是可绑定的。 为什么数据绑定机制看不到从其他接口继承的属性?

编辑:SharePoint 新手是对的:数据绑定在代码中手动定义时起作用。但是,我尝试使用 BindingSource 组件在设计器中定义数据绑定。当您将对象源添加到项目并将其指向 IChild 接口时,只有 ChildText 可见以定义绑定。

我更新了问题的标题以更好地反映我的问题。

【问题讨论】:

  • 你确定吗?它应该绑定没有任何问题。过去曾这样做过。

标签: .net winforms data-binding bindingsource


【解决方案1】:

对此并不完全确定,但这里是:

因为 IChild 只继承自 IParent,所以它看不到 ParentText 的实现(即 get 访问器),因为它存在于 Parent,而不是 IParent。因此,绑定到 IChild 不会继承 ParentText 属性。

事实上,我完全不确定您为什么要绑定到接口。您可以完成哪些无法绑定到 Child 的绑定到 IChild 的操作?

【讨论】:

  • 我有很多 IChild 的实现。我的视图(表单)不知道确切的实现,所以我需要使用接口。
  • Matthew,与 ChildText 属性相同的逻辑,但 ChildText 对数据绑定可见
【解决方案2】:

我认为,框架中有一个错误会导致所描述的问题。这是相关的连接问题:

https://connect.microsoft.com/VisualStudio/feedback/details/431273/interface-inheritance-bug

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 2011-04-25
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 2014-02-03
    • 2017-02-06
    相关资源
    最近更新 更多