接口实现了不同的接口,导致绑定到Combobox的问题 [英] Interface implements a different interface, causing issues binding to Combobox
问题描述
我有一个基类和一个基本界面:
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屋!