From f08871fe7d148d667f2f11f80f64de71a0d9504c Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期一, 15 六月 2026 18:19:37 +0800
Subject: [PATCH] 优化标准老化界面

---
 HMI.UI/DpUtil/FormGroupHelper.cs |   69 ++++++++++++++++++++++++++--------
 1 files changed, 52 insertions(+), 17 deletions(-)

diff --git a/HMI.UI/DpUtil/FormGroupHelper.cs b/HMI.UI/DpUtil/FormGroupHelper.cs
index 3447885..bc45159 100644
--- a/HMI.UI/DpUtil/FormGroupHelper.cs
+++ b/HMI.UI/DpUtil/FormGroupHelper.cs
@@ -1,11 +1,10 @@
-锘縰sing System;
-using System.ComponentModel;
+锘縰sing HMI.Core.Util;
+using Panuon.WPF.UI;
+using System.Collections.Generic;
 using System.Windows;
 using System.Windows.Controls;
-using Panuon.WPF.UI;
-using Panuon.WPF.UI.Configurations;
 
-namespace HMI.UI
+namespace HMI.UI.DpUtil
 {
     public class FormGroupHelper
     {
@@ -24,15 +23,14 @@
             new PropertyMetadata(false, (d, e) =>
             {
                 if (!(d is FormGroup fg)) return;
-                if ((bool)e.NewValue)
+                if ((bool)e.NewValue && !string.IsNullOrEmpty(fg.Header?.ToString()))
                 {
-                    var style = Application.Current.FindResource("RequiredInputFormGroupStyle") as Style;
-                    fg.Style = style;
+                    var dataTemplate = Application.Current.FindResource("RequiredFormHeader") as DataTemplate;
+                    fg.HeaderTemplate = dataTemplate;
                 }
                 else
                 {
-                    var style = Application.Current.FindResource("RequiredInputFormGroupStyle") as Style;
-                    fg.Style = style;
+                    fg.HeaderTemplate = null;
                 }
             }));
 
@@ -52,7 +50,7 @@
             DependencyProperty.RegisterAttached("ValidateMethod", typeof(ValidateMethodDelegate), typeof(FormGroupHelper),
             new PropertyMetadata(null));
 
-        public static bool ValidateForm(StackPanel fgPanel)
+        public static bool ValidateForm(StackPanel fgPanel, Dictionary<string, object> values = null)
         {
             if (fgPanel.Children == null) return true;
             bool res = true;
@@ -60,12 +58,29 @@
             {
                 if (child is FormGroup fg)
                 {
-                    object content = fg.Content;
-                    object value = GetValidateValue(content);
-                    if (value != null && GetIsRequired(fg) && string.IsNullOrEmpty(value.ToString()))
+                    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 = $"{fg.Header}涓嶈兘涓虹┖";
+                        fg.Message = string.Format(LangUtil.Get("App.CantBeEmpty"), fg.Header);
                         res = false;
                     }
                     else
@@ -90,11 +105,31 @@
                 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.Text;
+            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 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