接口实现了不同的接口,导致绑定到Combobox的问题 [英] Interface implements a different interface, causing issues binding to Combobox

查看:201
本文介绍了接口实现了不同的接口,导致绑定到Combobox的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习流利的NHibernate,这个问题源于该项目。



我有一个基类和一个基本界面:

  public abstract class Base:IBase 
{
public virtual Guid Id {get;组; }
public virtual bool IsValid()
{
return false;
}
}

public interface IBase
{
Guid id {get;组; }
bool IsValid();
}

我继承了我所有的其他域类:

  public class Item:Base,IItem 
{
public virtual string Name {get;组; }

public override bool IsValid()
{
< snip>
}
< snip>
}

public interface IItem:IBase
{
string Name {get;组; }
< snip>
}

但是,当我尝试将所有项目的列表绑定到winforms组合框I得到一个错误。

  var ds =从GetSession()中的i。Linq< IItem>()select i; 
cmbItems.DataSource = ds.ToArray();

this.cmbItems.DisplayMember =Name;
this.cmbItems.ValueMember =Id;

我收到错误:


无法绑定到新的值成员。
参数名称:值


我发现这是因为我在IItem上实现了IBase。如果我修改IItem它可以正常工作。

  public interface IItem:IBase 
{
Guid id {get ;组; }
string Name {get;组; }
< snip>
bool IsValid();
}

所以超出实际,只是让它工作,我正在做接口正确?我应该没有接口实现其他接口吗?如果我应该有IItem实现IBase,有没有办法正确绑定到Winforms控件?

解决方案

我认为这是因为WinForms绑定系统是基于使用 TypeDescriptor TypeDescriptor.GetProperties(typeof(IItem))仅返回声明的属性...所以 ComboBox 找到名称,因为它在 IItem ,但不是 Id



要解决此问题,您可以创建一个匿名类型您需要的属性:

  var ds = GetSession()中的i。Linq< IItem&())选择新的{i.Id , 我的名字 }; 
cmbItems.DataSource = ds.ToArray();

this.cmbItems.DisplayMember =Name;
this.cmbItems.ValueMember =Id;

无论如何,我不认为你应该重新声明 Id IsValid IItem 中,因为它会隐藏在 IBase (编译器在你这样做时会给你一个警告)


I am learning Fluent NHibernate and this issue arose from that project.

I have a base Class and a base Interface:

public abstract class Base : IBase
{
    public virtual Guid Id { get; set; }
    public virtual bool IsValid()
    {
        return false;
    }
}

public interface IBase
{
    Guid Id { get; set; }
    bool IsValid();
}

which I inherit all of my other Domain classes from:

public class Item:Base, IItem
{
    public virtual string Name { get; set; }

    public override bool IsValid()
    {
        <snip>
    }
    <snip>
}

public interface IItem: IBase
{
    string Name { get; set; }
    <snip>
}

However when I try and bind a list of all Items to a winforms Combobox I get an error.

        var ds = from i in GetSession().Linq<IItem>() select i;
        cmbItems.DataSource = ds.ToArray();

        this.cmbItems.DisplayMember = "Name";
        this.cmbItems.ValueMember = "Id";

I get an error:

Cannot bind to the new value member. Parameter name: value

I have figured out that this occurs because I am implementing IBase on IItem. If I modify IItem it works fine.

public interface IItem: IBase
{
    Guid Id { get; set; }
    string Name { get; set; }
    <snip>
    bool IsValid();
}

So beyond the practical, just make it work, am I doing interfaces correctly? Should I not have interfaces implement other interfaces? If I should have IItem implement IBase, is there a way to bind properly to a Winforms control?

解决方案

I think that's because WinForms binding system is based on the use of TypeDescriptor, and TypeDescriptor.GetProperties(typeof(IItem)) returns only the declared properties... So the ComboBox finds Name because it's declared in IItem, but not Id.

To work around this problem, you could create an anonymous type with the properties you need:

    var ds = from i in GetSession().Linq<IItem>() select new { i.Id, i.Name };
    cmbItems.DataSource = ds.ToArray();

    this.cmbItems.DisplayMember = "Name";
    this.cmbItems.ValueMember = "Id";

Anyway, I don't think you should redeclare Id and IsValid in IItem, because it would hide the properties declared in IBase (the compiler gives you a warning when you do that)

这篇关于接口实现了不同的接口,导致绑定到Combobox的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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