alielie
2025-03-13 a3ba25607612818c28d421bfa8eb2f92339d71ac
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
using HMI.Core.Util;
using Panuon.WPF.UI;
using System.Windows;
using System.Windows.Controls;
 
namespace HMI.UI.DpUtil
{
    public class FormGroupHelper
    {
        public static bool GetIsRequired(FormGroup fg)
        {
            return (bool)fg.GetValue(IsRequiredProperty);
        }
 
        public static void SetIsRequired(FormGroup fg, bool value)
        {
            fg.SetValue(IsRequiredProperty, value);
        }
 
        public static readonly DependencyProperty IsRequiredProperty =
            DependencyProperty.RegisterAttached("IsRequired", typeof(bool), typeof(FormGroupHelper),
            new PropertyMetadata(false, (d, e) =>
            {
                if (!(d is FormGroup fg)) return;
                if ((bool)e.NewValue && !string.IsNullOrEmpty(fg.Header?.ToString()))
                {
                    var dataTemplate = Application.Current.FindResource("RequiredFormHeader") as DataTemplate;
                    fg.HeaderTemplate = dataTemplate;
                }
                else
                {
                    fg.HeaderTemplate = null;
                }
            }));
 
 
        public delegate bool ValidateMethodDelegate(object value, out string msg);
        public static ValidateMethodDelegate GetValidateMethod(FormGroup fg)
        {
            return (ValidateMethodDelegate)fg.GetValue(ValidateMethodProperty);
        }
 
        public static void SetValidateMethod(FormGroup fg, ValidateMethodDelegate value)
        {
            fg.SetValue(ValidateMethodProperty, value);
        }
 
        public static readonly DependencyProperty ValidateMethodProperty =
            DependencyProperty.RegisterAttached("ValidateMethod", typeof(ValidateMethodDelegate), typeof(FormGroupHelper),
            new PropertyMetadata(null));
 
        public static bool ValidateForm(StackPanel fgPanel)
        {
            if (fgPanel.Children == null) return true;
            bool res = true;
            foreach (var child in fgPanel.Children)
            {
                if (child is FormGroup fg)
                {
                    fg.ValidateResult = ValidateResult.None;
                    fg.Message = null;
                    object content = fg.Content;
                    object value = GetValidateValue(content);
                    if (GetIsRequired(fg) && (value == null || string.IsNullOrEmpty(value.ToString())))
                    {
                        fg.ValidateResult = ValidateResult.Error;
                        fg.Message = string.Format(LangUtil.Get("App.CantBeEmpty"), fg.Header);
                        res = false;
                    }
                    else
                    {
                        ValidateMethodDelegate method = GetValidateMethod(fg);
                        if (method != null && !method(value, out string err))
                        {
                            fg.ValidateResult = ValidateResult.Error;
                            fg.Message = err;
                            res = false;
                        }
                    }
                }
            }
            return res;
        }
 
        private static object GetValidateValue(object content)
        {
            if (content == null) return null;
            if (content is UIElementCollection elementCollection)
                return elementCollection.Count > 0 ? GetValidateValue(elementCollection[0]) : null;
            if (content is TextBox tb) return tb.Text;
            if (content is CheckBox cb) return cb.IsChecked;
            if (content is ComboBox cBox) return cBox.SelectedItem ?? cBox.SelectedValue ?? cBox.Text;
 
            if (content is Panel panel) return GetValidateValue(panel.Children);
            if (content is NumberInput numberInput) return numberInput.Value;
            if (content is PasswordBox passwordInput) return passwordInput.Password;
            return null;
        }
    }
}