wenxiangchao
4 天以前 2e889843b4c171f2d6ad8e0d53084040703d11f6
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 HMI.Core.Manager;
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
 
namespace HMI.StdControl
{
    /// <summary>
    /// StdEnableMes.xaml 的交互逻辑
    /// </summary>
    public partial class StdSettingCheckBox : UserControl, INotifyPropertyChanged
    {
 
        #region ...
        public virtual event PropertyChangedEventHandler PropertyChanged;
 
        protected virtual void Set([CallerMemberName] string name = null)
        {
 
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
 
        protected void Set<T>(ref T old, T @new, [CallerMemberName] string propertyName = null)
        {
            old = @new;
            if (propertyName != null)
            {
                Set(propertyName);
            }
        }
        #endregion
 
        private event EventHandler<StdUIEventArg> valueChanged;
        public event EventHandler<StdUIEventArg> ValueChanged
        {
            add
            {
                valueChanged += value;
            }
            remove
            {
                valueChanged -= value;
            }
        }
 
        public static DependencyProperty IsCheckedProperty = DependencyProperty.Register(
                            "IsChecked",
                            typeof(bool),
                            typeof(StdSettingCheckBox),
                            new FrameworkPropertyMetadata(false));
 
        public static DependencyProperty ValueProperty = DependencyProperty.Register(
                            "Value",
                            typeof(string),
                            typeof(StdSettingCheckBox),
                            new FrameworkPropertyMetadata(null));
 
        public static DependencyProperty CbEnabledProperty = DependencyProperty.Register(
                            "CbEnabled",
                            typeof(bool),
                            typeof(StdSettingCheckBox),
                            new FrameworkPropertyMetadata(true));
 
        public static DependencyProperty TextProperty = DependencyProperty.Register(
                            "Text",
                            typeof(string),
                            typeof(StdSettingCheckBox),
                            new FrameworkPropertyMetadata(null));
 
 
        public bool CbEnabled
        {
            get => (bool)GetValue(CbEnabledProperty);
            set
            {
                SetValue(CbEnabledProperty, value);
            }
        }
 
        public string Value
        {
            get => (string)GetValue(ValueProperty);
            set
            {
                SetValue(ValueProperty, value);
            }
        }
 
        public string Text
        {
            get => (string)GetValue(TextProperty);
            set
            {
                SetValue(TextProperty, value);
            }
        }
 
        public bool IsChecked
        {
            get => (bool)GetValue(IsCheckedProperty);
            set
            {
                if (WriteConfig(Value, value))
                {
                    SetValue(IsCheckedProperty, value);
                }
            }
        }
 
        public StdSettingCheckBox()
        {
            InitializeComponent();
            this.Loaded += StdSettingCheckBox_Loaded;
        }
 
        private void StdSettingCheckBox_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                var value = ParameterManager.GetCommonParameterByName(Value);
                if (value == null)
                {
                    this.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    IsChecked = bool.Parse(value.Value);
                }
            }
            catch
            {
                IsChecked = false;
            }
        }
 
        private bool WriteConfig(string name, bool value)
        {
            try
            {
                valueChanged?.Invoke(this, new StdUIEventArg()
                {
                    StringValue = name,
                    BoolValue = value
                });
                return ParameterManager.UpdateCommonParameter(name, value, out string errMsg);
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}