wenxiangchao
2026-11-02 fe2aa4880d5c9fd88088ad0ed81ebaca05629f5f
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
using HMI.Core.Entity;
using HMI.Core.Util;
using HMI.UI.DpUtil;
using HMI.UI.Util;
using Microsoft.Win32;
using NPOI.SS.Formula.Functions;
using NPOI.SS.Formula.PTG;
using Panuon.WPF.UI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Controls;
using System.Windows.Interop;
 
namespace HMI.ParameterManage
{
    public class BoolData
    {
        public int Index { get; }
        public bool Value { get; set; }
 
        public BoolData(int index)
        {
            this.Index = index;
        }
    }
 
    public class IntData
    {
        public int Index { get; }
        public int Value { get; set; }
 
        public IntData(int index)
        {
            this.Index = index;
        }
    }
 
    public class DoubleData
    {
        public int Index { get; }
        public double Value { get; set; }
 
        public DoubleData(int index)
        {
            this.Index = index;
        }
    }
 
    public class StringData
    {
        public int Index { get; }
        public string Value { get; set; } = string.Empty;
 
        public StringData(int index)
        {
            this.Index = index;
        }
    }
 
    /// <summary>
    /// ModalWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SettingModal : WindowX
    {
        public SettingModal()
        {
            this.DataContext = this;
            InitializeComponent();
        }
 
        public ParameterData Data { get; set; }
        private ObservableCollection<ParameterData> Datasource;
 
        private bool _isProcessing;
 
        protected override void OnClosing(CancelEventArgs e)
        {
            //Press Continue Button
            if (DialogResult == true)
            {
                if (_isProcessing)
                {
                    e.Cancel = true;
                    return;
                }
                var okButton = WindowXModalDialog.GetOKButton(this);
                try
                {
                    _isProcessing = true;
                    ButtonHelper.SetIsPending(okButton, true);
                    if (!ValidateForm())
                    {
                        e.Cancel = true;
                        return;
                    }
 
                    SaveOrUpdateParameter(Data);
                }
                catch (Exception ex)
                {
                    ToastHelper.Error(this, $"Error: {ex.Message}");
                    e.Cancel = true;
                    return;
                }
                finally
                {
                    _isProcessing = false;
                    ButtonHelper.SetIsPending(okButton, false);
                }
            }
            base.OnClosing(e);
        }
 
        public bool ValidateName(object value, out string err)
        {
            err = null;
 
            string pattern = @"^[\p{L}_][\p{L}\p{N}_]*$";
            if (!Regex.IsMatch(value.ToString(), pattern))
            {
                err = "参数名不能以数字开头";
                return false;
            }
 
            var data = Datasource?.FirstOrDefault(e => e.Name == value.ToString());
            if (data != null && data != Data)
            {
                err = this.lang("App.MustBeUnique", this.lang("Pm.Parameter.Name"));
                return false;
            }
            return true;
        }
 
        private void SaveOrUpdateParameter(ParameterData data)
        {
        }
 
        private bool ValidateForm()
        {
            bool flag = FormGroupHelper.ValidateForm(formPanel, new Dictionary<string, object> {
                { "FgValue",GetTargetValue()}
            });
            if (flag)
            {
                Data.Name = TbName.Text;
                Data.Type = CbType.Text;
                Data.IsArray = CbIsArray.IsChecked == true;
                Data.Length = int.Parse(NiLength.Text);
 
                Data.Value = GetTargetValue();
                Data.Unit = TbUnit.Text;
                Data.Description = TbDescription.Text;
                Data.PGroup = TbGroup.Text;
                Data.Source = TbSource.Text;
            }
            return flag;
        }
 
        public bool? Open(ParameterData data, ObservableCollection<ParameterData> datasource, List<string> groups,string productName = null)
        {
            if (!string.IsNullOrEmpty(productName))
            {
                FgProductName.Visibility = System.Windows.Visibility.Visible;
                TbProductName.Text = productName;
            }
 
            Datasource = datasource;
            Data = data;
            TbGroup.ItemsSource = groups;
 
            TbName.Text = Data.Name;
            CbType.SelectedValue = Data.Type;
            CbIsArray.IsChecked = Data.IsArray;
            NiLength.Text = Data.Length.ToString();
 
            UpdateWindow(Data.Type);
            UpdateForm(Data);
 
            TbUnit.Text = Data.Unit;
            TbDescription.Text = Data.Description;
            TbGroup.Text = Data.PGroup;
            TbSource.Text = Data.Source;
            return this.ShowDialog();
        }
 
        private string GetTargetValue()
        {
            var type = CbType.Text;
            if (CbIsArray.IsChecked == true)
            {
                var arr = (object[])arrayInput.ItemsSource;
                return string.Join(",", arr.Select(e => e.GetType().GetProperty("Value").GetValue(e).ToString()).ToArray());
            }
 
            if (type == "Int" || type == "Double")
            {
                return NiNumberValue.Text;
            }
            else if (type == "Bool")
            {
                return CbBoolValue.Text;
            }
            else
            {
                return TbStringValue.Text;
            }
        }
 
        private void UpdateForm(ParameterData data)
        {
            if (data.IsArray)
            {
                string[] arr = data.Value.Split(',');
                arrayInput.ItemsSource = RefreshArray(arr, Data.Type);
            }
            else
            {
                if (Data.Type == "Int" || Data.Type == "Int")
                {
                    NiNumberValue.Text = Data.Value;
                }
                else if (Data.Type == "Bool")
                {
                    CbBoolValue.SelectedValue = Data.Value;
                }
                else
                {
                    TbStringValue.Text = Data.Value;
                }
            }
        }
 
        private void UpdateWindow(string type)
        {
            if (type == "Int" || type == "Double")
            {
                NiNumberValue.Visibility = System.Windows.Visibility.Visible;
                StringValueGrid.Visibility = System.Windows.Visibility.Collapsed;
                CbBoolValue.Visibility = System.Windows.Visibility.Collapsed;
            }
            else if (type == "Bool")
            {
                NiNumberValue.Visibility = System.Windows.Visibility.Collapsed;
                StringValueGrid.Visibility = System.Windows.Visibility.Collapsed;
                CbBoolValue.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                NiNumberValue.Visibility = System.Windows.Visibility.Collapsed;
                StringValueGrid.Visibility = System.Windows.Visibility.Visible;
                CbBoolValue.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
 
        private void CbType_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            string val = null;
            if (e.AddedItems != null && e.AddedItems.Count > 0)
            {
                val = ((ComboBoxItem)e.AddedItems[0]).Content.ToString();
            }
            UpdateWindow(val);
            if (CbIsArray.IsChecked == true)
            {
                arrayInput.ItemsSource = RefreshArray(GetLength(), val);
            }
        }
 
        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == true)
            {
                TbStringValue.Text = ofd.FileName;
            }
        }
 
        private void CbIsArray_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            bool isChecked = ((CheckBox)sender).IsChecked == true;
            if (isChecked)
            {
                FgLength.Visibility = System.Windows.Visibility.Visible;
                arrayInput.Visibility = System.Windows.Visibility.Visible;
                arrayInput.ItemsSource = RefreshArray(GetLength(), CbType.Text);
            }
            else
            {
                FgLength.Visibility = System.Windows.Visibility.Collapsed;
                arrayInput.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
 
        private object[] RefreshArray(int length, string type)
        {
            var arr = new object[length];
            for (int i = 0; i < length; i++)
            {
                if (type == "Int")
                {
                    arr[i] = new IntData(i);
                }
                else if (type == "Double")
                {
                    arr[i] = new DoubleData(i);
                }
                else if (type == "Bool")
                {
                    arr[i] = new BoolData(i);
                }
                else
                {
                    arr[i] = new StringData(i);
                }
            }
            return arr;
        }
 
        private object[] RefreshArray(string[] source, string type)
        {
            var arr = new object[source.Length];
            type = string.IsNullOrEmpty(type) ? "String" : type;
            for (int i = 0; i < arr.Length; i++)
            {
                if (type == "Int")
                {
                    arr[i] = new IntData(i)
                    {
                        Value = int.Parse(source[i])
                    };
                }
                else if (type == "Double")
                {
                    arr[i] = new DoubleData(i)
                    {
                        Value = double.Parse(source[i])
                    };
                }
                else if (type == "Bool")
                {
                    arr[i] = new BoolData(i)
                    {
                        Value = bool.Parse(source[i])
                    };
                }
                else
                {
                    arr[i] = new StringData(i)
                    {
                        Value = source[i]
                    };
                }
            }
            return arr;
        }
 
        private int GetLength()
        {
            try
            {
                string text = NiLength.Text;
                if (text.Contains("."))
                {
                    text = text.Split('.')[0];
                }
                int length = int.Parse(text);
                return length > 0 ? length : 1;
            }
            catch
            {
                return 1;
            }
        }
 
        private void NiLength_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (CbIsArray.IsChecked != true)
            {
                return;
            }
            arrayInput.ItemsSource = RefreshArray(GetLength(), CbType.Text);
        }
    }
}