using DevExpress.XtraEditors; using System; using System.ComponentModel; using System.Linq; namespace IStation.WinFrmUI.Monitor { /// /// 单一映射控件 /// public partial class SingleMappingMatrixDlg : XtraForm { public SingleMappingMatrixDlg() { InitializeComponent(); } public event Func ReloadDataEvent; private BindingList _bindingList = null; /// /// 绑定数据 /// public void Set(string paras, Model.SignalType signalType) { var list = Model.SingleMappingMatrix.ToList(paras); if (list == null) list = new System.Collections.Generic.List(); _bindingList = new BindingList(list); if (signalType != null) { var formatParas = signalType.GetEnumFormatParas(); if (formatParas != null && formatParas.Items != null) { this.repImgCmbValue.BeginUpdate(); this.repImgCmbValue.Items.Clear(); foreach (var item in formatParas.Items) { this.repImgCmbValue.Items.Add(item.Name, (double)item.Value, -1); } this.repImgCmbValue.EndUpdate(); } } this.bindingSource1.DataSource = _bindingList; this.bindingSource1.ResetBindings(false); } //验证 public bool Valid() { this.gridView1.CloseEditor(); this.gridView1.UpdateCurrentRow(); if (_bindingList.Count > 0) { for (int i = 0; i < _bindingList.Count; i++) { var vm = _bindingList[i]; if (string.IsNullOrEmpty(vm.Value.ToString())) { this.gridView1.FocusedRowHandle = i; this.gridView1.SetColumnError(colValue, "必填项"); //XtraMessageBox.Show("请将值映射信息设置完整!"); return false; } } } return true; } #region GridView //删除 private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e) { if (_bindingList == null || _bindingList.Count < 1) return; var row = this.gridView1.GetFocusedRow() as Model.SingleMappingMatrix; if (row == null) return; if (e.Column == this.colDelete) _bindingList.Remove(row); } //新增属性时必须先输入属性名 private void gridView1_ShowingEditor(object sender, CancelEventArgs e) { if (_bindingList == null) { e.Cancel = true; return; } if (this.gridView1.FocusedColumn != this.colKey) { var row = this.gridView1.GetFocusedRow() as Model.SingleMappingMatrix; if (row == null) { e.Cancel = true; return; } } } //验证名称的唯一性 private void gridView1_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e) { if (_bindingList == null || _bindingList.Count < 1) return; if (this.gridView1.FocusedColumn == this.colKey) { var row = this.gridView1.GetFocusedRow() as Model.SingleMappingMatrix; var list = _bindingList.ToList(); list.Remove(row); System.Text.RegularExpressions.Regex rex = new System.Text.RegularExpressions.Regex(@"^\d+$"); if (rex.IsMatch(e.Value.ToString())) if (list.Find(x => x.Key == Convert.ToDouble(e.Value)) != null) { e.ErrorText = "值映射重复"; e.Valid = false; } } } #endregion private void btnOk_Click(object sender, EventArgs e) { if (!Valid()) return; string json = string.Empty; if (_bindingList.Any()) { json = Model.SingleMappingMatrix.ToJson(_bindingList.ToList()); } if (this.ReloadDataEvent != null) { var bol = this.ReloadDataEvent(json); if (!bol) { XtraMessageBox.Show("设置失败!"); return; } XtraMessageBox.Show("设置成功!"); } this.DialogResult = System.Windows.Forms.DialogResult.OK; this.Close(); } } }