Shuxia Ning
2024-10-08 cf4967a0aebab18c5a37137f3e4c61b2d73a54bb
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
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
 
namespace IStation.WinFrmUI
{
    /// <summary>
    ///  
    /// </summary>
    public partial class EnumFormatParasCtrl : UserControl, IFormatParas
    {
        public EnumFormatParasCtrl()
        {
            InitializeComponent();
        }
 
        private Model.SignalType.EnumFormatParas _model = null;
        private BindingList<Model.SignalType.EnumFormatItem> _allBindingList = null;
 
 
        //初始化数据
        public void Set(string paras)
        {
            _model = Model.SignalType.EnumFormatParas.ToModel(paras);
            if (_model == null)
                _model = new Model.SignalType.EnumFormatParas();
            if (_model.Items == null)
                _model.Items = new List<Model.SignalType.EnumFormatItem>();
 
            _allBindingList = new BindingList<Model.SignalType.EnumFormatItem>(_model.Items);
            this.gridControl1.DataSource = _allBindingList;
            this.gridControl1.RefreshDataSource();
        }
 
        //单元格点击事件
        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (_allBindingList == null || _allBindingList.Count < 1)
                return;
            //删除
            var index = this.gridView1.FocusedRowHandle;
            if (e.Column == this.colDelete)
            {
                _allBindingList.RemoveAt(index);
            }
        }
 
        //新增值时必须先输入名称
        private void gridView1_ShowingEditor(object sender, CancelEventArgs e)
        {
            if (_allBindingList == null)
            {
                e.Cancel = true;
                return;
            }
            if (this.gridView1.FocusedColumn != this.colName)
            {
                var row = this.gridView1.GetCurrentViewModel(_allBindingList);
                if (row == null)
                {
                    e.Cancel = true;
                    return;
                }
            }
        }
 
        //验证 Value 唯一性
        private void gridView1_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
        {
            if (_allBindingList == null || _allBindingList.Count < 1)
                return;
            if (this.gridView1.FocusedColumn == this.colValue)
            {
                var row = this.gridView1.GetCurrentViewModel(_allBindingList);
                var list = _allBindingList.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.Value == Convert.ToInt32(e.Value)) != null)
                    {
                        e.ErrorText = "值映射重复";
                        e.Valid = false;
                    }
            }
        }
 
 
        //验证
        public bool Valid()
        {
            if (_allBindingList == null || _allBindingList.Count < 1)
            {
                XtraMessageBox.Show("请输入枚举项!");
                return false;
            }
 
            return true;
        }
 
 
        //获取
        public string Get()
        {
            if (!Valid())
                return default;
            _model.Items = new List<Model.SignalType.EnumFormatItem>(_allBindingList);
            return _model.ToJson();
        }
 
 
    }
}