alielie
2025-03-26 4cac1b533e133fdc8b3def90a0f998820f51907a
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
using HMI.Core.Entity;
using HMI.Core.Util;
using HMI.UI.DpUtil;
using HMI.UI.Util;
using Panuon.WPF.UI;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
 
namespace HMI.ParameterManage
{
    /// <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;
            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);
            if (flag)
            {
                Data.Name = TbName.Text;
                Data.Type = CbType.Text;
                Data.Value = TbValue.Text;
                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)
        {
            Datasource = datasource;
            Data = data;
            TbName.Text = Data.Name;
            CbType.SelectedValue = Data.Type;
            TbValue.Text = Data.Value;
            TbUnit.Text = Data.Unit;
            TbDescription.Text = Data.Description;
            TbGroup.Text = Data.PGroup;
            TbSource.Text = Data.Source;
            return this.ShowDialog();
        }
    }
}