接口绑定的问题

发现一个问题,各位兄弟们看看。偶是新手,多多指教啊。


实体类以及相关接口定义如下:
   public interface ISerialObject {
                 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;//调试时,看内存,居然只有RptName,Name好像不可见
       f.Name = "22";//但是可以直接修改Name

      Binding nameBinding = this.textBox1.DataBindings.Add("Text", bindSrc,  “RptName", true);//正常运行
      nameBinding = this.textBox2.DataBindings.Add("Text", bindSrc, "Name", true);//异常 ,报错。
}

分析:

    MS不支持。

 

解决办法:

   1、IChem与ISerialObject合了

   2、在IChem增加 int UID { get; }
                 string Name { get; set; }
      相当于只申明有此接口。

 

posted @ 2009-06-29 22:09  雷猪头  阅读(231)  评论(0编辑  收藏  举报