From 9d1660270272180cd71147d6ddd53d1382643d2e Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期一, 02 十一月 2026 18:00:14 +0800
Subject: [PATCH] merge: 并入远程 Initial commit 作为历史根节点

---
 HMI.UI/DpUtil/FormGroupHelper.cs |  135 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 135 insertions(+), 0 deletions(-)

diff --git a/HMI.UI/DpUtil/FormGroupHelper.cs b/HMI.UI/DpUtil/FormGroupHelper.cs
new file mode 100644
index 0000000..bc45159
--- /dev/null
+++ b/HMI.UI/DpUtil/FormGroupHelper.cs
@@ -0,0 +1,135 @@
+锘縰sing HMI.Core.Util;
+using Panuon.WPF.UI;
+using System.Collections.Generic;
+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, Dictionary<string, object> values = null)
+        {
+            if (fgPanel.Children == null) return true;
+            bool res = true;
+            foreach (var child in fgPanel.Children)
+            {
+                if (child is FormGroup fg)
+                {
+                    string fgName = fg.Name;
+                    if (fg.Visibility != Visibility.Visible)
+                    {
+                        ClearValue(fg);
+                        continue;
+                    }
+                    fg.ValidateResult = ValidateResult.None;
+                    fg.Message = null;
+                    object value;
+                    if (values != null && values.ContainsKey(fgName))
+                    {
+                        value = values[fgName];
+                    }
+                    else
+                    {
+                        object content = fg.Content;
+                        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 Panuon.WPF.UI.NumberInput numberInput) return numberInput.Value;
+            if (content is PasswordBox passwordInput) return passwordInput.Password;
+            return null;
+        }
+
+        private static void ClearValue(object content)
+        {
+            if (content == null) return;
+            if (content is UIElementCollection elementCollection && elementCollection.Count > 0)
+                ClearValue(elementCollection[0]);
+            if (content is TextBox tb) tb.Text = null;
+            if (content is CheckBox cb) cb.IsChecked = null;
+            if (content is ComboBox cBox)
+            {
+                cBox.SelectedItem = null;
+                cBox.SelectedValue = null;
+                cBox.Text = null;
+            }
+
+            if (content is Panel panel) ClearValue(panel.Children);
+            if (content is Panuon.WPF.UI.NumberInput numberInput) numberInput.Value = null;
+            if (content is PasswordBox passwordInput) passwordInput.Password = null;
+        }
+    }
+}

--
Gitblit v1.9.1