duheng
2025-03-28 b266e82b9a377fa35a766f7a3a2f5aa95f3c9125
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using DevExpress.XtraEditors;
using System;
using System.ComponentModel;
using System.Linq;
 
namespace IStation.WinFrmUI.Monitor
{
    /// <summary>
    /// 单一映射控件  
    /// </summary>
    public partial class SingleMappingMatrixDlg : XtraForm
    {
        public SingleMappingMatrixDlg()
        {
            InitializeComponent();
        }
 
 
        public event Func<string, bool> ReloadDataEvent;
        private BindingList<Model.SingleMappingMatrix> _bindingList = null;
 
        /// <summary>
        /// 绑定数据
        /// </summary> 
        public void Set(string paras, Model.SignalType signalType)
        {
            var list = Model.SingleMappingMatrix.ToList(paras);
            if (list == null)
                list = new System.Collections.Generic.List<Model.SingleMappingMatrix>();
            _bindingList = new BindingList<Model.SingleMappingMatrix>(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();
 
        }
 
    }
}