发现一个问题,各位兄弟们看看。偶是新手,多多指教啊。
实体类以及相关接口定义如下:
public interface IA {
int UID { get; }
string Name { get; set; }
}
public interface IChem : ISerialObject {
string RptName { get; set; }
}
public class Chem : IChem {
public Chem() {
id = ++no;
Name = id.ToString();
RptName = Name + Name;
}
private static int no = 0;
private int id;
public int UID {
get{ return id; }
}
private string name;
public string Name {
get { return name; }
set { name = value; }
}
private string rptName;
public string RptName {
get { return rptName; }
set { rptName = value; }
}
public static IChem Create()
{
return new Chem();
}
}
窗口有2个TextBox用来绑定数据源
private BindingSource bindSrc = new BindingSource();//定义为窗口成员变量
private void Form1_Load(object sender, EventArgs e)
{
BindingList<IChem> curr = new BindingList<IChem>();
curr.Add( Chem.Create() );
curr.Add(Chem.Create());
bindSrc.DataSource = curr;
ISerialObject d = (ISerialObject)curr[0];
IChem f = (IChem)d;
f.Name = "22";
Binding nameBinding = this.textBox1.DataBindings.Add("Text", bindSrc, "RptName", true);
nameBinding = this.textBox2.DataBindings.Add("Text", bindSrc, "Name", true);//异常 ,
}
我分析了下,主要是如果接口类Name不可见,无法绑定。怎么解决
挺奇怪,接口属性默认就是与接口同级的,能看到接口就能看到其属性呀,你试试添加接口的命名空间引用呢?