private void ctlDataBingByCtlName(Dictionary <string, MB.WinBase.Common.ColumnPropertyInfo> editCfgColumns, string[] dataPropertys, System.Windows.Forms.BindingSource bindingSource, System.Windows.Forms.Control.ControlCollection ctls, DataBindingOptions bindingOptions, List <ColumnBindingInfo> bindingDatas, Dictionary <string, MB.WinBase.Common.ColumnEditCfgInfo> columnEditInfoList) { foreach (Control ctl in ctls) { string ctlTypeName = ctl.GetType().Name; if (_DESCRIPTION_CONTROLS.Contains <string>(ctlTypeName)) { continue; } if (_CONTAINER_CONTROLS.Contains(ctlTypeName)) { ctlDataBingByCtlName(editCfgColumns, dataPropertys, bindingSource, ctl.Controls, bindingOptions, bindingDatas, columnEditInfoList); continue; } string sName = ctl.Name; if (sName.Length < MIN_CTL_NAME_LENGTH) { MB.Util.TraceEx.Write("在执行BindingSourceEx.SetDataSource 方法时,控件" + sName + "的命名方式至少要大于" + MIN_CTL_NAME_LENGTH + "个字符。该控件以及所在的ChildControl将不进行处理,请检查。", MB.Util.APPMessageType.SysWarning); } if (sName.Length <= MIN_CTL_NAME_LENGTH) { ctlDataBingByCtlName(editCfgColumns, dataPropertys, bindingSource, ctl.Controls, bindingOptions, bindingDatas, columnEditInfoList); continue; } //获取控件需要进行绑定的属性名称。 string ctlBindingName = getCtlBindingPropertyName(ctl); string dataPropertyName = sName.Substring(CTL_LEFT_PREX_LENGTH, sName.Length - CTL_LEFT_PREX_LENGTH); if (Array.IndexOf <string>(dataPropertys, dataPropertyName) < 0) { continue; } if (editCfgColumns != null && editCfgColumns.ContainsKey(dataPropertyName)) { MB.WinBase.Common.ColumnPropertyInfo colCfg = editCfgColumns[dataPropertyName]; ColumnBindingInfo ctlBinding = new ColumnBindingInfo(dataPropertyName, colCfg, ctl); bindingDatas.Add(ctlBinding); } System.Windows.Forms.Binding binding = new System.Windows.Forms.Binding(ctlBindingName, bindingSource, dataPropertyName, false, DataSourceUpdateMode.OnValidation); //编辑界面的数据绑定分3步来完成 //1,先绑定ComboBox 的数据源 if (columnEditInfoList != null && columnEditInfoList.Count > 0) { FillComboxLookUp(ctl, dataPropertyName, editCfgColumns, columnEditInfoList, false); } //2,绑定点击选择数据的数据源 //3,控件编辑绑定 ctl.DataBindings.Add(binding); } }
//绑定ButtonClick #region 内部函数处理... //控件编辑绑定。 private void ctlDataBingByDataBinding(Dictionary <string, MB.WinBase.Common.ColumnPropertyInfo> editCfgColumns, string[] dataPropertys, System.Windows.Forms.BindingSource bindingSource, MB.WinBase.Binding.IDataBindingProvider dataBindingProvider, DataBindingOptions bindingOptions, List <ColumnBindingInfo> bindingDatas, Dictionary <string, MB.WinBase.Common.ColumnEditCfgInfo> columnEditInfoList) { if (dataBindingProvider == null || dataBindingProvider.DataBindings == null || dataBindingProvider.DataBindings.Count == 0) { return; } Dictionary <Control, DesignColumnXmlCfgInfo> bindingCtls = dataBindingProvider.DataBindings; foreach (Control ctl in bindingCtls.Keys) { string ctlTypeName = ctl.GetType().Name; if (_DESCRIPTION_CONTROLS.Contains <string>(ctlTypeName)) { continue; } string ctlBindingName = getCtlBindingPropertyName(ctl); string dataPropertyName = bindingCtls[ctl].ColumnName; if (Array.IndexOf <string>(dataPropertys, dataPropertyName) < 0) { continue; } MB.WinBase.Common.ColumnPropertyInfo colCfg = null; if (editCfgColumns.ContainsKey(dataPropertyName)) { colCfg = editCfgColumns[dataPropertyName]; } if (editCfgColumns != null && editCfgColumns.ContainsKey(dataPropertyName)) { //特殊处理,绑定到特殊的控件。 ColumnBindingInfo ctlBinding = new ColumnBindingInfo(dataPropertyName, colCfg, ctl); if (!colCfg.CanEdit) { setEditControlReadonly(ctl, true); } //如果是string 类型 并且设置MacLength 同时绑订的是TextBox 控件,那么就限制它的输入长度 if (string.Compare(colCfg.DataType, "System.String", true) == 0) { TextBox txtBox = ctl as TextBox; if (txtBox != null && colCfg.MaxLength > 0) { txtBox.MaxLength = colCfg.MaxLength; } } bindingDatas.Add(ctlBinding); } System.Windows.Forms.Binding binding = new System.Windows.Forms.Binding(ctlBindingName, bindingSource, dataPropertyName, true, DataSourceUpdateMode.OnPropertyChanged); //DataSourceUpdateMode.OnValidation if (colCfg != null) { formateBindControl(ctl, binding, colCfg); } //编辑界面的数据绑定分3步来完成 //1,先绑定ComboBox 的数据源 if (columnEditInfoList != null && columnEditInfoList.Count > 0) { FillComboxLookUp(ctl, dataPropertyName, editCfgColumns, columnEditInfoList, false); } //2,绑定点击选择数据的数据源 if (columnEditInfoList != null && columnEditInfoList.ContainsKey(dataPropertyName)) { bindingToSpecialEditCtl(ctl, columnEditInfoList[dataPropertyName]); } //3,控件编辑绑定 ctl.DataBindings.Add(binding); } }