/// <summary> /// 对数据控件实现双向绑定 /// </summary> /// <param name="controls">要搜索数据控件的窗体控件集合</param> public void BindDataControls(Control.ControlCollection controls) { var dataControls = MyWinForm.GetIBControls(controls); foreach (IDataControl control in dataControls) { //control.LinkObject 这里都是 "DataContext" object dataSource = GetInstanceByMemberName(control.LinkObject); if (control is TextBox) { ((TextBox)control).DataBindings.Add("Text", dataSource, control.LinkProperty); } else if (control is Label) { ((Label)control).DataBindings.Add("Text", dataSource, control.LinkProperty); } else if (control is ListBox) { ((ListBox)control).DataBindings.Add("SelectedValue", dataSource, control.LinkProperty, false, DataSourceUpdateMode.OnPropertyChanged); } else if (control is DateTimePicker) { ((DateTimePicker)control).DataBindings.Add("Value", dataSource, control.LinkProperty, false, DataSourceUpdateMode.OnPropertyChanged); } else { //自定义处理控件类型 BindDataControl(control, dataSource, control.LinkProperty); } } }
private void MvvmForm_Load(object sender, EventArgs e) { var ibControls = MyWinForm.GetIBControls(this.Controls); ControlCollection coll = new ControlCollection(this); foreach (IDataControl ctr in ibControls) { coll.Add(ctr as Control); } BindDataControls(coll); }
private void MvvmForm_Load(object sender, EventArgs e) { var ibControls = MyWinForm.GetIBControls(this.Controls); BindDataControls(ibControls); }