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
using HMI.Core.Entity;
using HMI.ParameterManage.Model;
using HMI.UI.DpUtil;
using HMI.UI.Util;
using Microsoft.Win32;
using Panuon.WPF.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Controls;
 
namespace HMI.ParameterManage
{
    /// <summary>
    /// ModalWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ParameterModal : WindowX
    {
        private XmlModel DataModel;
        public ParameterModal(XmlModel model)
        {
            DataModel = model;
            this.DataContext = this;
            InitializeComponent();
        }
 
        public ParameterData Data { get; set; }
 
        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;
                    }
 
                    DataModel.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)
        {
            return DataModel.ValidateParameterName(value, out err);
        }
 
        private bool ValidateForm()
        {
            var cache = new Dictionary<string, object> {
                { "FgValue",GetTargetValue()}
            };
            bool flag = FormGroupHelper.ValidateForm(formPanel, cache);
            if (flag)
            {
                Data.Name = TbName.Text;
                Data.Type = CbType.Text;
                Data.IsArray = CbIsArray.IsChecked == true;
                Data.Value = GetTargetValue();
                Data.Unit = TbUnit.Text;
                Data.Description = TbDescription.Text;
                Data.ProductName = TbProductName.Text;
                Data.PGroup = CbGroup.Text;
            }
            return flag;
        }
 
        public bool? Open(string productName, ParameterData data, List<string> groups)
        {
            Data = data;
            TbName.Text = Data.Name;
            CbType.SelectedValue = Data.Type;
            CbIsArray.IsChecked = Data.IsArray;
            CbGroup.ItemsSource = groups;
            UpdateWindow(Data.Type);
            UpdateForm(Data);
            TbUnit.Text = Data.Unit;
            TbProductName.Text = productName;
            TbDescription.Text = Data.Description;
            CbGroup.Text = Data.PGroup;
            return this.ShowDialog();
        }
 
        private string GetTargetValue()
        {
            var type = CbType.Text;
            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.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);
        }
 
        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == true)
            {
                TbStringValue.Text = ofd.FileName;
            }
        }
    }
}