Custom data binding, window form
来源:互联网 发布:奇迹哥 知乎 编辑:程序博客网 时间:2024/05/21 06:15
// custom ObjectPublic Class Customer{// class to get set a string to format of (key=value;key=value;...;...;)private NVParser parser = new NVParser();public Customer(){ parser.ParseText(this.description);}// the description perportiy is property which store all customer data as a string; eg (FirstName=xxx;Surname=xxx;age=24) it is not bindable// after those property binding, every time the control property (textbox.text or dropdownlist.selectedvalue) are changed, // the binding property (customer.age,firstname,surname) will // invoked. //************************// this function will not call automatically, if you want to get or set this property, you need call it manually in code(eg, save the customer button click) [Bindable(false)] public String Description { get { return parser.ToString(); } set { parser.ParseText(value); } }// property for binding to control (dropdownlist)[Bindable(true)] public int Age { get { if (!String.IsNullOrEmpty(parser["Age"])) { return Convert.ToInt32(parser["Age"]); } return 0; } set { parser["Age"] = value.ToString(); } }// property for binding to control (dropdownlist) [Bindable(true)] public string FirstName { get { return parser["FirstName"]; } set { parser["FirstName"] = value; } }[Bindable(true)] public string Surname { get { return parser["Surname"]; } set { parser["Surname"] = value; } }} // Customer Form// this form have a bingsource1.DataSource is customer object// this form contains textboxs, dropdownlist, need bind to customerpublic Class CustomerForm {public CustomerForm (){InitializeCompoent();}public CustomerForm (Customer) : this(){//bind bindingsource by VS// drag bindingsource control to form, select bindingsource, to go to property window, set datasouce to Customer.//bind control //drag textboxs, dropdownlist to form//textbox bind text to customer.firstname, customer.surname//dropdownlist bind selectedvalue to customer.age// fill drop downlist, must before set bindingSourcedropdwon.displaymember = customer.Age;dropdown.valuemember = cusomer.Age;dropdown.dataSource = yourAgeListArray; //eg {1,2,3...100...}dropdown.selectedIndex = -1; // select none// bind binding source to Customer object, // if the customer have value, it will set the dropdown, textbox value// else, set the dropdown mannually, the textbox.text will set to "" by default.bindingSource1.DataSource = Customer; // this if used to select the first item in the dropdown as default value if there is nothing select(the binding object is new create). if (DropDown.SelectedIndex == -1 && DropDown.Items.Count > 0) { // only manually change the dropdownlist.SelectedIndex property will not change the binding source. (bindSource.OnPropertyChanged event will not fire) // so must change the dropdownlist.SelectedIndex first, // then based on the dropdownlist.SelectedValue, change the binding source property DropDownTemplate.SelectedIndex = 0; if (DropDownTemplate.SelectedValue != null) { Data.Template = Convert.ToInt32(DropDownTemplate.SelectedValue); } }} private void btnOK_Click(object sender, EventArgs e) { // save ot get the customer.discription stringstring s = Description; }}