首页 新闻 会员 周边

接口属性无法绑定

0
[已解决问题] 解决于 2009-06-26 20:13

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

实体类以及相关接口定义如下:

   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不可见,无法绑定。怎么解决

 

问题补充: 发现一个问题,各位兄弟们看看。偶是新手,多多指教啊。 实体类以及相关接口定义如下: 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, f.Name = "22";//但是可以直接修改Name Binding nameBinding = this.textBox1.DataBindings.Add("Text", bindSrc, "RptName", true);//正常运行 nameBinding = this.textBox2.DataBindings.Add("Text", bindSrc, "Name", true);//异常 ,报错 } 我整理下代码格式,请大家看看 我全换成类,就可以
雷猪头的主页 雷猪头 | 初学一级 | 园豆:195
提问于:2009-06-26 17:50
< >
分享
最佳答案
0

挺奇怪,接口属性默认就是与接口同级的,能看到接口就能看到其属性呀,你试试添加接口的命名空间引用呢?

斯克迪亚 | 老鸟四级 |园豆:4124 | 2009-06-26 18:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册