Winforms数据绑定与接口继承 [英] Winforms databinding with interface inheritance

查看:149
本文介绍了Winforms数据绑定与接口继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我因为这个问题而改变应用程序域之前,我需要有人确认我所看到的内容。问题是当对彼此继承的接口进行数据绑定时,您无法在基接口上看到属性。

I need someone to confirm what I am seeing before I may a change to the domain of the application because of this issue. The issue is that when databinding against interfaces that inherit from one another, you can not see the properties on the base interfaces.

我正在使用数据绑定的WinForms应用程序。这是在.net3.5,不,我不能使用wpf。

I am working on a WinForms application that uses databinding. This is in .net3.5 and no I can not use wpf.

无论如何,我有这样的设置。

Anyways, I have a setup something like this.

public interface IClassOne
{
    string Prop1 { get; set; }
}

public interface IClassTwo : IClassOne
{
    string Prop2 { get; set; }
}

public abstract class ClassOne : IClassOne
{
    public string Prop1 { get; set; }
}

public class ClassTwo : ClassOne, IClassTwo
{
    public string Prop2 { get; set; }
}

基类将包含公共属性和逻辑。基本接口将具有这些公共属性,因此必须在每个具体实现上实现它们。

The base class would hold common properties and logic. The base interface would have those common properties on it so they would have to be implemented on each concrete implementation.

如果我是上面的类结构数据绑定,我会是绑定到IClassTwo。问题是当我数据绑定到IClassTwo时,我无法在WinForms的任何设计器操作中看到Prop1。即使我绕过该限制并将控件绑定到Prop1,它也不起作用。

If I was databinding to my class structure above, I would be binding to IClassTwo. The problem is when I databind to IClassTwo, I can not see Prop1 in any of the designer operations for WinForms. Even if I get around that limitation and get a control to be bound to Prop1, it does not work.

但是如果我绑定两个ClassTwo,那么数据绑定就可以了。

However if I bind two ClassTwo, then databinding works.

我不想处理混凝土因为这会使得使用模拟和测试太难。我也不想把所有内容都放在IClassTwo上,因为当我进行另一个具体的实现时,我将不得不重复代码。

I do not want to deal with the concrete classes because that would make using mocks and testing too hard. I also do not want to put everything on IClassTwo because I would have to repeat code when I make another concrete implementation.

我需要知道的是,如果这真的没有工作。如果你知道为什么,这将是一个奖金。

What I need to know is if this truly doesn't work. If you know why, that would be a bonus.

谢谢
Tony

Thank you Tony

推荐答案

http:// social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/4151e6b6-44f7-45e2-9a8e-92c4f8539095/?prof=required

以下是MSDN上发布的有关为什么不起作用的内容的嗤之以鼻。写这篇文章的海报是邮资。

Here is a snippit of what is posted on MSDN as to why this doesn't work. The poster who wrote this goes by "franking".


不,它不可能绑定到接口! BindingSource提供了一些使用类型作为数据源的帮助。在内部,它将使用类型信息实例化BindingList<>实例。此外,添加新项目也可以,但AddNew()将失败,除非您订阅了AddingNew事件。

No, it's not possible to bind to an interface! BindingSource offers some help to use a type as data source. Internally it will instantiate a BindingList<> instance using the type information. Also adding new items will work, but AddNew() will fail unless you subscribe the AddingNew event.

这篇关于Winforms数据绑定与接口继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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