日期:2014-05-18  浏览次数:20850 次

TextBox控件怎么绑定到类的属性
定义类:
public class BindData
{
public string name;
public int age;

  public BindData()
{
//
name = "John";
age = 25;
}
}
private void btnBind_Click(object sender, System.EventArgs e)
{
BindData bindData = new BindData ();
this.txtName.DataBindings.Add("Text",bindData,"name");
this.txtAge .DataBindings .Add("Text",bindData,"age");
}
出错“无法绑定到DataSource”的属性或列,各位高手教教我该怎么做。

------解决方案--------------------
帮顶
------解决方案--------------------
public class BindData
{
private string name;
private int age;

public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public BindData()
{
//
name = "John";
age = 25;
}


private void btnBind_Click(object sender, System.EventArgs e)
{
BindData bindData = new BindData ();
this.txtName.DataBindings.Add("Text",bindData,"Name");//是属性,不是Public变量
this.txtAge .DataBindings .Add("Text",bindData,"Age");
}
------解决方案--------------------
this.txtName.DataBindings.Add("Text",bindData,"Name");//是属性,不是Public变量 

hzg_1998不說我還不注意呢,謝謝!