瀏覽代碼

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

chenrongqian@kdanmobile.com 2 年之前
父節點
當前提交
181a5646ec
共有 1 個文件被更改,包括 39 次插入0 次删除
  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>