Ver Fonte

公用接口 - 根据控件名称查找控件

chenrongqian@kdanmobile.com há 2 anos atrás
pai
commit
181a5646ec
1 ficheiros alterados com 39 adições e 0 exclusões
  1. 39 0
      PDF Office/Helper/CommonHelper.cs

+ 39 - 0
PDF Office/Helper/CommonHelper.cs

@@ -173,6 +173,45 @@ namespace PDF_Office.Helper
             catch { return null; }
         }
 
+        public static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject
+
+        {
+
+            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
+
+            {
+
+                var child = VisualTreeHelper.GetChild(parent, i);
+
+                string controlName = child.GetValue(System.Windows.Controls.Control.NameProperty) as string;
+
+                if (controlName == name)
+
+                {
+
+                    return child as T;
+
+                }
+
+                else
+
+                {
+
+                    T result = FindVisualChildByName<T>(child, name);
+
+                    if (result != null)
+
+                        return result;
+
+                }
+
+            }
+
+            return null;
+
+        }
+
+
         /// <summary>
         /// 从页码集合获取页码字符串,如1,2,3 转换成1-3
         /// </summary>